What's the difference between 'weak' and 'assign' in delegate property declaration

前端 未结 1 514
醉话见心
醉话见心 2020-12-02 06:37

Whats the difference between this:

@property (nonatomic, weak) id   delegate; 

and this:

@property          


        
相关标签:
1条回答
  • 2020-12-02 06:58

    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.

    0 讨论(0)
提交回复
热议问题