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

Objective CIosIos5DelegatesProperties

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFirdousView Question on Stackoverflow
Solution 1 - Objective CyujiView Answer on Stackoverflow