如何理解哪个属性适合 NSDictionary 例如。保留还是分配?
现在我使用
@property (nonatomic, retain) NSDictionary *currentAttribute;
如果我使用复制或分配而不是保留,内存性能是否会有更大差异?
now i using
@property (nonatomic, retain) NSDictionary *currentAttribute;
if i use copy or assign instead of retain is it more difference in memory performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
声明它
copy
意味着您将获得一个全新的NSDictionary
对象,供您的类使用。如果它是一个相当大的字典,这可能会影响性能;不是很引人注目,但无论如何都很重要。通过保留
它,您只需为您的类提供指向同一NSDictionary
实例的自己的指针即可。如果
NSDictionary
自动释放,声明它assign
会使您的应用程序面临崩溃的风险。如果它最终进入池并因池将其保留计数减少到 0 而被释放,您的类将无法再访问它,从而导致崩溃。Declaring it
copy
would mean you get an entirely newNSDictionary
object for use with your class. If it's quite a large dictionary this can be a performance hit; not very noticeable, but significant anyway. Byretain
ing it, you simply give your class its own pointer to the sameNSDictionary
instance.Declaring it
assign
puts your application at risk of crashing in case theNSDictionary
is autoreleased. If it ends up in the pool and gets deallocated because the pool reduced its retain count to 0, your class won't get to access it anymore, causing a crash.