委托属性声明中的“弱"和“分配"有什么区别 [英] What's the difference between 'weak' and 'assign' in delegate property declaration
本文介绍了委托属性声明中的“弱"和“分配"有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有什么区别:
@property (nonatomic, weak) id <SubClassDelegate> delegate;
还有这个:
@property (nonatomic, assign) id <SubClassDelegate> delegate;
我想为委托使用属性.
推荐答案
weak
和 assign
的唯一区别在于,如果对象是一个 weak
code> 属性指向的被释放,那么weak
指针的值将被设置为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.
这篇关于委托属性声明中的“弱"和“分配"有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文