当“保留”时会发生什么? NSArray 还是 NSMutableArray?

发布于 2024-09-28 17:30:27 字数 290 浏览 1 评论 0原文

在源代码中,

@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 技术交流群。

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

发布评论

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

评论(1

素衣风尘叹 2024-10-05 17:30:27

是一样的。设置属性仅更改该数组的所有权,而不更改数组的内容(内容由同一数组拥有)。因此,只有数组需要被-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.

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