当“保留”时会发生什么? NSArray 还是 NSMutableArray?
在源代码中,
@property(retain) NSString* str;
@sythesize str;
self.str = newStr;
我知道实际上会发生以下情况
if( str != newStr ){
[str release];
str = [newStr retain];
}
那么 NSArray 或 NSMutableArray 的情况怎么样?看起来很复杂,应该考虑浅复制和深复制。
In the source codes
@property(retain) NSString* str;
@sythesize str;
self.str = newStr;
I understand actually following will happen
if( str != newStr ){
[str release];
str = [newStr retain];
}
So how about the case for NSArray or NSMutableArray ? Seem like it is complex,shallow copy and deep copy should be considered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是一样的。设置属性仅更改该数组的所有权,而不更改数组的内容(内容由同一数组拥有)。因此,只有数组需要被
-retain
'ed。事实上,运行时并不关心属性的具体 Objective-C 类型。相同的 setter 过程将应用于每个
@property(retain)
属性。要使 setter 执行浅复制,请将其设为
@property(copy)
。没有办法让它深复制。It's the same. Setting a property only changes the ownership of that array, not the content of the array (the content is owned by the same array). Therefore, only the array needs to be
-retain
'ed.In fact, the runtime doesn't care about the specific Objective-C type of the property. The same setter procedure will be applied to every
@property(retain)
properties.To make the setter perform shallow copying, make it
@property(copy)
. There no way to make it deep-copy.