title: How should I unregister for KVO on self?
body: <p>I'm trying to use KVO to observe properties on a <code>UIView</code> subclass in order to trigger drawing by calling <code>drawRect:</code>. In my <code>initWithFrame:</code>, I have this:</p>
  
  <pre><code>...
  self.observedKeysThatTriggerRedraw = [NSArray arrayWithObjects:@"name", nil];
  for (NSString *aKey in self.observedKeysThatTriggerRedraw) {
      [self observeValueForKeyPath:aKey ofObject:self change:nil context:redrawContextString];
  }
  ...
  </code></pre>
  
  <p>(<code>redrawContextString</code> is a constant <code>NSString</code> unique to this class)</p>
  
  <p>The KVO notifications are firing as they should, which is triggering the redraw correctly. The problem is unregistering KVO. If I don't unregister, everything runs fine, but I get an exception if I put this at the top of my <code>dealloc</code>:</p>
  
  <pre><code>for (NSString *aKey in self.observedKeysThatTriggerRedraw) {
      [self removeObserver:self forKeyPath:aKey];
  }
  
  self.name = nil;
  ...
  [super dealloc];
  </code></pre>
  
  <p>I get this message in the console and a crash when it gets to the <code>removeObserver:forKeyPath:</code>:</p>
  
  <blockquote>
    <p><em>CoreAnimation: ignoring exception: Cannot remove an observer &lt;MyViewClass 0x5b47210> for the key path "name" from &lt;MyViewClass 0x5b47210> because it is not registered as an observer</em></p>
  </blockquote>
  
  <p>Is there some trick to unregistering KVO when you are observing <code>self</code>? Are my observers being unregistered for me be the time <code>dealloc</code> is called? I've read in a bunch of places that you shouldn't unregister for KVO in <code>dealloc</code>, but I'm not sure where else I can do it when observing <code>self</code>.</p>