当我使用访问器方法设置对象的属性时到底发生了什么?

发布于 2024-11-29 12:12:20 字数 552 浏览 1 评论 0原文

假设我有一个名为 MyClass 的对象,它有一个定义为 @property (nonatomic, keep) NSString *foo; 的属性,我合成了该属性。

然后在另一个类中,假设应用程序委托我定义了一个字符串(但它可以是任何东西) NSString *myString = [[NSString alloc] initWithString:@"Hi"]; 并调用一个实例MyClass: [myClass setFoo:myString];

实际发生了什么?是否设置了对为 myString 分配的空间的引用?或者它是否获取为 myString 分配的内存中的内容并将其设置为为 foo 分配的内存,因为我在 上调用了 retain >foo

我必须在应用程序委托中释放 myString 。我必须在 MyClass 中释放 foo ,因为它被保留了,但是由于另一个分配的变量被分配给它,我是否必须再次释放它?

Say I have an object called MyClass, which has a property defined as @property (nonatomic, retain) NSString *foo; and I synthesize that property.

Then in another class, say the application delegate I define a string (but it could be anything) NSString *myString = [[NSString alloc] initWithString:@"Hi"]; and call on an instance of MyClass: [myClass setFoo:myString];

What actually happens? Does the reference to the space that was allocated for myString get set? Or does it get what is in the allocated memory for myString and set it to the allocated memory for foo, since I called retain on foo?

And I have to release myString in the application delegate. And I have to release foo in MyClass, since it was retained, but do I have to release it again since another alloc'd variable was assigned to it?

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

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

发布评论

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

评论(3

灰色世界里的红玫瑰 2024-12-06 12:12:20

这完全取决于财产的申报方式。因为你的是(retain),它会保留相同的对象指针,并且还会自动发送-retain,这样无论调用者是否持有一个对象,它都可以确保对内存的访问。参考。如果您使用(copy),它将发送-copy,这将创建一个新对象,该对象是旧对象的副本(前提是该对象符合“NSCopying”)。

这一切都发生在附件实现中,通常是在您使用 @synthesize 时为您生成的。如果您自己实现,请务必使用正确的行为!

It all depends how the property is declared. Because yours is (retain), it will keep the same object pointer, and also automatically send -retain so it can ensure access to the memory whether or not the caller holds a reference. If you used (copy), it would send -copy which would make a new object that's a copy of the old one (provided the object conforms to `NSCopying).

This all happens in the accessories implementation, which is commonly generated for you when you use @synthesize. If you implement your own, be sure to use the correct behavior!

固执像三岁 2024-12-06 12:12:20

@synthesize 基本上为您创建了两种方法。在您的情况下,这两种方法与此类似:

-(NSString*) foo;
{
    return foo; //synthesized ivar
}

-(void) setFoo:(NSString*)newValue;
{
    if(foo != newValue){
        [foo release];
        foo = [newValue retain];
    }
}

但是,@synthesize 不会释放 dealloc 中的值,因此您必须手动执行此操作。

Objective-C 中的对象被视为指针,因此当您说 NSString* a = b; 时,您并不是在复制对象本身,而是创建了另一个指向该对象的指针。合成属性也不例外。

@synthesize basically creates two methods for you. In your case those two methods would be similar to this:

-(NSString*) foo;
{
    return foo; //synthesized ivar
}

-(void) setFoo:(NSString*)newValue;
{
    if(foo != newValue){
        [foo release];
        foo = [newValue retain];
    }
}

However, @synthesize won't release the value in dealloc, so you have to do that manually.

Objects in Objective-C are treated as pointers, so when you say NSString* a = b; you're not copying the object itself, you're just making another pointer to the object. Synthesized properties are no exception to this.

因为看清所以看轻 2024-12-06 12:12:20

发生的情况是,当您执行 [myClass setFoo:myString]; 时,myString 指向的对象的保留计数会加一。

最初,当您分配一个字符串并存储在 myString 中引用,保留计数为 1。将其存储在像您一样具有保留的属性中,使其保留计数增加到 2。

不,您不必再次释放它。最大保留计数为 2。因此有 2 个版本。

What happens is that the object myString points to gets its retain count incremented by one when you do [myClass setFoo:myString];

Initially, when you alloc a string and store the reference in myString, the retain count is 1. Storing it in a property with retain like you have, gets its retain count incremented to 2.

No you don't have to release it again. Max retain count is 2. Hence 2 releases.

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