在我目前正在进行的一个项目中,我正在研究前辈的代码。我在这里遇到的一件事是,有这样的 setter/getter :
- (NSDictionary *)userInfo
{
return [[userInfo retain] autorelease];
}
显然 userInfo 已经被类保留了,我不明白的是:retain-autoreleasing 这个对象的附加值是什么?这种方法有什么区别:
- (NSDictionary *)userInfo
{
return userInfo;
}
干杯,
EP。
最佳答案
想象一下:
id x = [[A alloc] init];
NSDictionary *userInfo = [x userInfo];
[x release];
// Do something with userInfo ...
// Would fail if the getter did not retain/autorelease.
如果 getter 没有执行保留/自动释放操作,则会失败,因为当 x
被释放时,userInfo
也会被释放。
关于objective-c - 保留/自动释放已保留的属性有什么额外好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5679479/