在iPhone SDK的所有可变类中使用copy是否比retain更好?
我开始知道,最好在 NSString 的 @property 上使用复制而不是保留,因为如果提供 NSMutableString 可能会遇到问题。同样的条件适用于 NSArray 、 NSDictionary 、 NSMutableData 吗?
I came to know that, it is better to use copy on @property of NSString than retain due to the problems you can get if you are provided with a NSMutableString. Is the same condition holds true for NSArray , NSDictionary, NSMutableData ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保留时出现的问题我假设您的意思是数据可能会意外更改。
如果上述情况对于您使用字符串或集合的情况来说是意外的,我会考虑使用复制。因此,您可以维护字符串、集合或其他对象的已知副本,这些副本在您不知情的情况下不会更改。
但是,可能还有其他原因在非可变集合或字符串上使用保留而不是复制。您可以接受内容可能会发生变化,但是在属性中使用 NSArray 来表明使用容器的对象不会修改它本身。
我认为 NSString 属性可能更想使用复制。系列可能会有所不同,请查看您的要求并看看什么最适合您的情况。
By problems when retaining I'm assuming you mean that the data may unexpectedly change.
I would consider using copy if the above is unexpected for the circumstances that you are using the string or collection. So you can maintain a known copy of the string, collection or other object that is not going to change without you knowing about it.
However, there may be other reasons to use retain rather than copy on a non-mutable collection or string. You may accept that the contents may change, however use NSArray in the property to show that the object using the container will not modify it itself.
I would think the NSString properties probably want to use copy more so than not. Collections may be different, look at your requirements and see what fits best in your situation.