dealloc中的属性:release然后设置为nil?或简单地释放 [英] Properties in dealloc: release then set to nil? or simply release

查看:79
本文介绍了dealloc中的属性:release然后设置为nil?或简单地释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C(和Stackoverflow)的新手,对于属性方面的最佳实践我也有所了解.

I'm new to Objective-C (and stackoverflow) and I'm a little turned around about best practices with regards to properties.

我的理解是,当您完全使用完某个属性后,可以通过释放错误并立即将其设置为nil来避免错误,以便后续消息也返回nil而不是异常.

My understanding is that when you're completely done with a property you can avoid bugs by releasing them and then immediately setting to nil so that subsequent messages also return nil instead of an exception.

[myProperty版本],myProperty = nil;

[myProperty release], myProperty = nil;

但是,当涉及到为'copy'和'retain'属性释放分配时,是否需要同时进行这两项工作?还是做一个简单的

However, when it comes to dealloc for 'copy' and 'retain' properties is there any need to do both? or does a simple

[myProperty版本] 剪了它?另外,我是否不需要在dealloc中释放'assign'属性?

[myProperty release] cut it? Also, am I correct that I don't need to release 'assign' properties in dealloc?

谢谢!

推荐答案

请释放,但不要将其设置为nil.通过@synthesized设置器设置为零:

Do release, but don't bother setting to nil. Setting to nil via your @synthesized setter:

self.myProperty = nil

将在重新分配过程中释放您的旧值(尽管如注释中所述,可能会有不希望的副作用),但只需将nil分配给您的成员变量即可:

will release your old value as part of the reassignment (though as noted in the comments, may have unwanted side effects), but simply assigning nil to your member variable:

myProperty = nil

不会.

[myProperty release]

是您所需要的.

(关于分配"属性,您是正确的.)

(and you are correct about "assign" properties.)

这篇关于dealloc中的属性:release然后设置为nil?或简单地释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆