[英]What's the difference between 'weak' and 'assign' in delegate property declaration
Whats the difference between this: 这有什么区别:
@property (nonatomic, weak) id <SubClassDelegate> delegate;
and this: 还有这个:
@property (nonatomic, assign) id <SubClassDelegate> delegate;
I want to use property for delegates. 我想为代表使用属性。
The only difference between weak
and assign
is that if the object a weak
property points to is deallocated, then the value of the weak
pointer will be set to nil
, so that you never run the risk of accessing garbage. weak
和assign
之间的唯一区别是,如果weak
属性指向的对象被释放,那么weak
指针的值将被设置为nil
,这样你就不会冒着访问垃圾的风险。 If you use assign
, that won't happen, so if the object gets deallocated from under you and you try to access it, you will access garbage. 如果你使用
assign
,那就不会发生,所以如果对象从你下面取消分配并且你试图访问它,你将访问垃圾。
For Objective-C objects, if you're in an environment where you can use weak
, then you should use it. 对于Objective-C对象,如果您处于可以使用
weak
的环境中,那么您应该使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.