解释对象变量的声明和赋值

发布于 2024-12-13 07:56:34 字数 240 浏览 0 评论 0 原文

在此代码片段中:

NSString *testString; 
testString = [[NSString alloc] init]; 

为什么在第二行,我们不必编写 *testString = ... 来访问它实际指向的位置?

第一行之后,什么是*testString,什么是testString

In this snippet:

NSString *testString; 
testString = [[NSString alloc] init]; 

Why, on the second line, do we not have to write *testString = ... in order to access the location where it's actually pointing?

After the first line, what is *testString and what is testString?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生面具三千个 2024-12-20 07:56:34

第一行创建 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.

吖咩 2024-12-20 07:56:34

所有对象都是通过指针引用的。第一行

NSString * testString;

声明实例变量。如果你的变量类型是一个对象(除了类型 id),你需要 *

从那时起对 testString 的引用是指针

如果你创建 2 个字符串。创建了 2 个物理对象(在内存中)

NSString * testString = [[NSString alloc] init];
NSString * testString2 = [[NSString alloc] init];

//setting testString to testString2 will lose the pointer to testString for good

testString = testString2;  //<--bad if you still care about testString (and leaks the memory too)

我建议查看 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

NSString * testString;

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)

NSString * testString = [[NSString alloc] init];
NSString * testString2 = [[NSString alloc] init];

//setting testString to testString2 will lose the pointer to testString for good

testString = testString2;  //<--bad if you still care about testString (and leaks the memory too)

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

岁月苍老的讽刺 2024-12-20 07:56:34

为什么在第二行,我们不必编写 *testString = ... 来访问它实际指向的位置?

init 方法返回一个指向对象的通用指针——它的返回类型是id。 testString 是一个指向 NSString 的指针,它是一个对象,因此您将一个指针分配给另一个指针。取消引用分配给的指针将导致类型不匹配。

变量名是放置某些内容的位置(内存地址的标签)。变量的类型是您可以放在那里的类型。对于指针,您放入其中的东西也是内存地址。为了获取该地址,您需要取消引用该指针。您可以在该地址中放置的内容与您在指针本身中放置的内容不同。

第一行之后,什么是*testString,什么是testString

在第一行之后,*testString,或者testString指向的东西,是垃圾(实际上是未定义的)。 testString 是一个指向内存地址的指针(4 或 8 个字节,具体取决于您的系统),并且它也是未定义的。

在第二行之后,*testString是一个NSString对象。 testString 仍然是一个指向地址的指针,其中存在有效的 NSString 对象。

Why, on the second line, do we not have to write *testString = ... in order to access the location where it's actually pointing?

The init method returns a generic pointer to an object -- its return type is id. testString is a pointer to an NSString, 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, what is *testString and what is testString?

After the first line, *testString, or the thing at which testString 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 an NSString object. testString is still a pointer to an address, where there is a valid NSString object.

千纸鹤 2024-12-20 07:56:34

这只是因为我们影响了指针。

[[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 every NSObject 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文