委托属性声明中'weak'和'assign'之间的区别是什么 [英] What's the difference between 'weak' and 'assign' in delegate property declaration
本文介绍了委托属性声明中'weak'和'assign'之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有什么区别:
@property (nonatomic, weak) id <SubClassDelegate> delegate;
这个:
@property (nonatomic, assign) id <SubClassDelegate> delegate;
我想为代表使用属性。
推荐答案
弱
和 assign
之间的唯一区别是,如果对象一个弱
属性指向已取消分配,然后弱
指针的值将设置为 nil
,这样你就不会冒着访问垃圾的风险。如果你使用 assign
,那就不会发生,所以如果对象从你下面取消分配并且你试图访问它,你将访问垃圾。
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. 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.
对于Objective-C对象,如果你在一个可以使用 weak
的环境中,那么你应该使用它。
For Objective-C objects, if you're in an environment where you can use weak
, then you should use it.
这篇关于委托属性声明中'weak'和'assign'之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文