title: How to properly check if non-Optional return value is valid?
body: <p>I've run into an odd case when trying to check a return value, and I'm wondering how to do this "properly", in the Swift sense.</p>
  
  <p>I have an <code>NSStatusItem</code> (named <code>item</code>), and I'm trying to assign the <code>NSStatusItem</code> an <code>NSImage</code>. When I make the <code>NSImage</code>, since I pass it a string value for the image name, I want to make sure that the <code>NSImage</code> is actually valid (what if I mistype the image name string?).</p>
  
  <p>The first thing I tried was this:</p>
  
  <pre><code>if let image: NSImage? = NSImage(named: "CorrectIconName") {
      item.image = image
  }
  </code></pre>
  
  <p>But this gives the error "Bound value in a conditional binding must be of Optional type". I thought that saying <code>image: NSImage?</code> made it clear that it was Optional, but I guess not.</p>
  
  <p>I changed that to this:</p>
  
  <pre><code>let image: NSImage? = NSImage(named: "CorrectIconName")
  if image {
       item.image = image
  }
  </code></pre>
  
  <p>Which works totally fine. But I don't get why this works, while the first example doesn't. It seems more-or-less to be the exact same thing. And since the first one didn't compile, I thought I'd try some other routes...</p>
  
  <p>Since <code>NSImage(named:)</code> does return an <code>NSImage</code> and not an <code>NSImage?</code>, I thought I'd see what happened if I assigned the return value of the constructor directly to <code>item</code>:</p>
  
  <pre><code>item.image = NSImage(named: "CorrectIconName")
  </code></pre>
  
  <p>Which works, but doesn't allow for the error checking I want to do. If the string is wrong, the <code>NSStatusItem</code> gets <code>nil</code> for an image, which leads to me having an invisible status bar item.</p>
  
  <p>Next, I tried this:</p>
  
  <pre><code>let image: NSImage = NSImage(named: "CorrectIconName")
  if image {
      item.image = image
  }
  </code></pre>
  
  <p>But this gives the error "Type 'NSImage' does not confirm to protocol 'LogicValue'", which I guess means you aren't allowed to check if it's <code>nil</code> or not with an if statement.</p>
  
  <p>However, you can check whether it is nil by doing the following:</p>
  
  <pre><code>let image: NSImage = NSImage(named: "CorrectIconName")
  if image != nil {
      item.image = image
  }
  </code></pre>
  
  <p>So, here's the question: how exactly is one <em>supposed</em> to check a return value if it isn't Optional?</p>