Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Automatic Reference Counting (ARC) and Key-Value Observing

I'm writing an Objective-C application using ARC and it's definitely a great improvement even though I never really minded retain counts. However, there's one case I couldn't find in the documentation on first glance.

If, in my init method, I call [self addObserver:self forKeyPath:…] to get notifications of when an ivar declared using an @property directive, do I need to explicitly call removeObserver… in the dealloc method, or does ARC handle key value observer cleanup? Similarly if I add my object to a NSNotificationCenter, does that need to be explicitly removed in dealloc?

like image 455
Justin Mrkva Avatar asked Nov 14 '11 14:11

Justin Mrkva


1 Answers

You need to call removeObserver, ARC only automates retain counts. removeObserver does not impact the retain count

like image 58
zaph Avatar answered Sep 24 '22 02:09

zaph