Weak Reference Set To Nil, But What About Unsafe Unretained?


In the book, you mention that when the object a weak reference points to is dealloc’d, the pointer is set to nil. But apparently, you can’t use “weak” when targeting iOS 4, even with ARC turned on. Am I correct in that?

I was reading that you could use __unsafe _unretained instead, but it does NOT automatically nil out a delloc’d reference. How exactly would you handle that? Do you override dealloc to fix that (but I guess the pointer wouldn’t receive the dealloc message?), or some other method? Or is this just not a particular concern?

Thanks for any advice.


You’re correct about dealloc.

Any object that is pointed to by __unsafe_unretained pointers (such as delegating objects, objects with target/action pairs, and the NSNotificationCenter) must have those pointers nullified before your object deallocates.

The best way to do this is by implementing -dealloc to handle the logic for you.

There is a discussion in Chapter 24 on this topic, in case it’s still unclear.


Thanks for the insight - this is one of the few programing books I’ve read where the answers to my unanswered questions are usually just a few chapters ahead… Great job :slight_smile:


We’re glad that you’ve been enjoying it so far. Please don’t hesitate to let us know if you get stuck further down the road.