title: How to safely removeObserver (Swift)
body: <p>I have added an observer</p>
  
  <pre><code>override func viewDidLoad()
  {
      super.viewDidLoad()
  
      NSNotificationCenter.defaultCenter().addObserver(self, selector:"selector name", name: "observer name", object:nil)
      ...
  }
  </code></pre>
  
  <p>When removing observer in deinit, </p>
  
  <pre><code>deinit
  {
      NSNotificationCenter.defaultCenter().removeObserver(self, forKeyPath: &lt;some string&gt;)
  }
  </code></pre>
  
  <p>the app sometimes <strong>crashes</strong>:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'NSRangeException', reason:
    'Cannot remove an observer "class"
    for the key path "some string" from
    NSNotificationCenter because it is not registered as an
    observer.</p>
  </blockquote>
  
  <hr>
  
  <p>So I am trying to add do/catch</p>
  
  <pre><code>deinit
  {
      do{
          try NSNotificationCenter.defaultCenter().removeObserver(self, forKeyPath: &lt;some string&gt;)
      }catch{}
  }
  </code></pre>
  
  <p>But I get a warning:</p>
  
  <blockquote>
    <p>catch block is unreachable because no errors are thrown in do block</p>
  </blockquote>
  
  <p><strong>And the app crashes</strong></p>
  
  <hr>
  
  <p>and when I am adding a try</p>
  
  <pre><code>deinit
  {
      do{
          try NSNotificationCenter.defaultCenter().removeObserver(self, forKeyPath: &lt;some string&gt;)
      }catch{}
  }
  </code></pre>
  
  <p>I get this warning:</p>
  
  <blockquote>
    <p>no calls to throwing functions occur within try expresion</p>
  </blockquote>
  
  <p><strong>And the app crashes</strong></p>
  
  <p>How should that be done? </p>