问题描述
这有什么区别:
@property(nonatomic,weak)id< SubClassDelegate>代表;
而这个:
@property(nonatomic,assign)id< SubClassDelegate>代表;
我想为代理使用属性。
weak
和 assign
之间的唯一区别是,如果对象一个 weak
属性指向被释放,那么 weak
指针的值将被设置为 nil
,以免您无法访问垃圾的风险。如果您使用 assign
,那将不会发生,所以如果对象从你下面被释放,并尝试访问它,你将访问垃圾。
对于Objective-C对象,如果您处于可以使用 weak
的环境中,那么应该使用它。 p>
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. 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.
For Objective-C objects, if you're in an environment where you can use weak
, then you should use it.
这篇关于代理属性声明中的“weak”和“assign”有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!