title: Getting current NSPopUpButton's selection with titleOfSelectedItem returns "optional value"
body: <p>Edit: I figured out how to prevent this, but I still have a question as to why it returns an optional value. You may skip to the end.</p>
  
  <p>I'm quite new to Swift, and I'm getting behavior I can't understand. Let's say I drag a popup button called <code>myButton</code> into the ViewController. I want to print the currently selected item to the console. Here's how I would do it:</p>
  
  <pre><code>  @IBOutlet weak var myButton: NSPopUpButton!
  
    override func viewDidLoad() {
      super.viewDidLoad()
  
      let myVariable = myButton.titleOfSelectedItem
      print(myVariable)
  
      // Do any additional setup after loading the view.
    }
  </code></pre>
  
  <p>I expect <code>Item1</code> to be printed, as that's the option selected by default when the view loads. However, I actually get <code>Optional("Item 1")</code>.</p>
  
  <p>This has been causing a few problems for me. For example, the <code>print(myVariable)</code> line gives me this cryptic error:</p>
  
  <blockquote>
    <p>Expression explicitly coerced from 'String?' to Any</p>
  </blockquote>
  
  <p>Also, I can't do something like this:</p>
  
  <pre><code>if myButton.titleOfSelectedItem == "Item1" || "Item3" {
    let currentSelection = "odd"
  } else {
    let currentSelection = "even"
  }
  </code></pre>
  
  <p>as I get a bunch of errors – because of the <code>||</code> and the <code>else</code> as far as I can tell, even though I think it should work fine.</p>
  
  <p>I've tried searching for why this occurs, but couldn't find any explanations. From the warning, it seems like when I get the selection with <code>titleOfSelectedItem</code>, it gives me an <em>optional</em> value. This doesn't make sense, as a menu item <em>has</em> to be selected. Its value can't be <code>nil</code>.</p>
  
  <p>I looked up a bunch of tutorials to see how they implement popup button functionality. The only thing I could see was when someone</p>
  
  <ol>
  <li>Made an array</li>
  <li>Removed all items from the popup button with <code>func removeAllItems()</code></li>
  <li>Added items to the popup button from the array with <code>func addItems(withTitles: [String])</code></li>
  <li>Obtained the index of the selected item with <code>var indexOfSelectedItem: Int</code></li>
  <li>Retrieved the respective value from the array</li>
  </ol>
  
  <p>and <em>then</em> used that. This, however, seems unnecessarily complicated and I can't understand why I wouldn't be able to get just the title of the selected popup item simply with <code>myButton.titleOfSelectedItem</code>. Can anyone suggest to me what to do?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>So I realized you need to "unwrap" the optional value to make it a normal value, which is as simple as adding a <code>!</code> to the end of the variable, as follows:</p>
  
  <pre><code>  @IBOutlet weak var myButton: NSPopUpButton!
  
    override func viewDidLoad() {
      super.viewDidLoad()
  
      let myVariable = myButton.titleOfSelectedItem!
      print(myVariable)
  
      // Do any additional setup after loading the view.
    }
  </code></pre>
  
  <p>Now there's no error, and <code>Item1</code> is printed.</p>
  
  <p>What I can't yet understand is, why was an optional value printed in the first place? There are three items in the NSPopUpButton. Any one of the three <em>has</em> to be selected. There's no opportunity for <code>myButton.titleOfSelectedButton</code> to be <code>nil</code>. Why then do I need to unwrap it to use it as a string with <code>myButton.titleOfSelectedButton!</code> if it's not optional?</p>