https://softwareengineering.stackexchange.com/questions/229287/what-use-is-a-non-zeroing-weak-reference
weak reference – a reference that DOES NOT affect the reference count. Simply put, a weak reference is a reference (pointer, in Objective-C land) to an object which does not participate in keeping that object alive.
Weak references are useful for things like avoiding retain cycles, but their utility is limited due to their inherent danger. With a plain weak reference in Objective-C, when the target object is destroyed, you’re left with a dangling pointer. (your weak reference points to trash content) If your code tries to use that pointer, it will crash or worse.
Use unsafe__unretained to create non-zeroing weak pointers in order to clarify this:
1 |
NSObject * __unsafe_unretained unsafeReference; |
or
1 |
@property (unsafe_unretained) NSObject *unsafeProperty; |
An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated. This means that you’ll be left with a dangling pointer to the memory originally occupied by the now deallocated object, hence the term “unsafe.” Sending a message to a dangling pointer will result in a crash.
Zeroing weak references (weak references are only available in Mac OS X ≥ 10.7 and iOS ≥ 5) eliminate this danger. They work just like a regular unsafe unretained reference, except that when the target object is destroyed, they automatically become nil. At any time you access an object through a zeroing weak reference, you’re guaranteed to either access a valid, live object, or get nil. As long as your code can handle nil, then you’re perfectly safe.
You can just declare any instance variable like so:
1 |
__weak id _foo; |
And it’s automatically a zeroing weak reference.
strong reference – a reference that DOES affect the reference count