委托属性声明中的“弱”和“赋值”的区别是什么

这两者有什么区别:

@property (nonatomic, weak) id  <SubClassDelegate> delegate;

还有这个:

@property (nonatomic, assign) id  <SubClassDelegate> delegate;

我想为委托使用属性。

21112 次浏览

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.