解释对象变量的声明和赋值
在此代码片段中:
NSString *testString;
testString = [[NSString alloc] init];
为什么在第二行,我们不必编写 *testString = ...
来访问它实际指向的位置?
第一行之后,什么是*testString
,什么是testString
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一行创建 NSString 类型的指针。 C++ 和 Objective-C 中的指针在声明时用星号 (*) 字符表示。第二行你说这个名为“testString”的指针引用了你在内存中分配的 NSString 对象的内存位置。
The first line you are creating the pointer of NSString type. Pointers in C++ and Objective-C are denoted by the asterisk (*) character when you declare them. The second line you are saying this pointer called "testString" references the memory location of the NSString object that you have allocated in memory.
所有对象都是通过指针引用的。第一行
声明实例变量。如果你的变量类型是一个对象(除了类型 id),你需要 *
从那时起对 testString 的引用是指针
如果你创建 2 个字符串。创建了 2 个物理对象(在内存中)
我建议查看 Apple 关于 Objective-C 的指南。具体来说,本节
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1
All objects are referred to by pointers. The first line
declares the instance variable. If your variable type is an object (aside from type id), you need the *
from then on the reference to testString is pointer
If you create 2 strings. 2 physical objects are created (in memory)
I recommend checking out Apple's guide on Objective-C. Specifically this section
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1
init
方法返回一个指向对象的通用指针——它的返回类型是id
。 testString 是一个指向 NSString 的指针,它是一个对象,因此您将一个指针分配给另一个指针。取消引用分配给的指针将导致类型不匹配。变量名是放置某些内容的位置(内存地址的标签)。变量的类型是您可以放在那里的类型。对于指针,您放入其中的东西也是内存地址。为了获取该地址,您需要取消引用该指针。您可以在该地址中放置的内容与您在指针本身中放置的内容不同。
在第一行之后,
*testString
,或者testString指向的东西,是垃圾(实际上是未定义的)。testString
是一个指向内存地址的指针(4 或 8 个字节,具体取决于您的系统),并且它也是未定义的。在第二行之后,
*testString
是一个NSString
对象。testString
仍然是一个指向地址的指针,其中存在有效的NSString
对象。The
init
method returns a generic pointer to an object -- its return type isid
.testString
is a pointer to anNSString
, which is an object, so you are assigning a pointer to another pointer. Dereferencing the assigned-to pointer would be a type mismatch.A variable name is a place (a label for a memory address) in which to put something. The type of the variable is the kind of thing that you can put there. In the case of a pointer, the kind of thing that you put in it is also a memory address. In order to get that address, you dereference the pointer. The kind of thing that you can put at that address is different from the kind that you put in the pointer itself.
After the first line,
*testString
, or the thing at whichtestString
points, is garbage (actually undefined).testString
is a pointer (4 or 8 bytes depending on your system) to a address in memory, and it is also undefined.After the second line,
*testString
is anNSString
object.testString
is still a pointer to an address, where there is a validNSString
object.这只是因为我们影响了指针。
[[NSString alloc] init]
返回一个指向 NSString 的指针。在Cocoa中,每个对象都是动态分配的(如C中的malloc),并且每个NSObject都是通过其指针/地址进行操作的(在这一点上,许多ObjC程序员不知道)甚至知道它们正在操作指针而不是对象)
That's simply because we affect the pointer.
[[NSString alloc] init]
returns a pointer to an NSString.In Cocoa every object is dynamically allocated (as in
malloc
in C) and everyNSObject
is manipulated thru its pointer/address (in such a point that many ObjC programmer don't even know that they are manipulating pointers and not objects)