开发者

When to write "NSString *str" or "NSString str"?

I'm learning Objective C to program on iOS. I know it has something to do with pointers, which I fail to understand. I don't know what's the difference of creating a string (or any NSObject) like this:

NSString place = ...;
or
NSString *place = ...;

Thanks for开发者_高级运维 the help!!


You never write NSString str. Period. All Obj-C objects are actually C pointers.


NSString is a class and as such instances of the class declared as NSString *str must always be declared as a pointer (since object instances can only be accessed via a pointer to the objects structure). Therefore this declaration is illegal: NSString str


Objective-C is, in some sense, more like a scripting language. Class definitions are maintained during runtime and can be modified. There is a C interface to the class definition system in objc.h. Even system classes can be modified, though it is not a good idea to do so. Because of this, all Objective-C objects must be created at runtime and accessed via pointers. To put it another way, there is no way for the compiler to know what an object should look like at compile time. This is also why "object may not respond to selector" is a warning, not an error and has the word "may" in it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜