title: How to unwrap an optional value from Any type?
body: <p>Given an array of <code>[Any]</code> that has a mix of optional and non optional values, e.g:</p>
  
  <pre><code>let int:Int? = 1
  let str:String? = "foo"
  
  let values:[Any] = [int,2,str,"bar"]
  </code></pre>
  
  <p>How can we extract the value of the <code>Optional</code> in the <code>Any</code> type (if there is one) so we can create a generic print function that only prints out the values.</p>
  
  <p>E.g. this printArray function goes through and prints each element:</p>
  
  <pre><code>func printArray(values:[Any]) {
      for i in 0..&lt;values.count {
          println("value[\(i)] = \(values[i])")
      }
  }
  
  printArray(values)
  </code></pre>
  
  <p>Which will output:</p>
  
  <pre><code>value[0] = Optional(1)
  value[1] = 2
  value[2] = Optional("foo")
  value[3] = bar
  </code></pre>
  
  <p>How can we change it so it only prints the underlying value so that it unwraps the value if it's Optional? e.g:</p>
  
  <pre><code>value[0] = 1
  value[1] = 2
  value[2] = foo
  value[3] = bar
  </code></pre>
  
  <hr>
  
  <h3>Update Progress...</h3>
  
  <p>It can work when changing the argument to <code>[Any?]</code>, e.g:</p>
  
  <pre><code>let values:[Any?] = [int,2,str,"bar"]
  
  func printArray(values:[Any?]) {
      for i in 0..&lt;values.count {
          println("value[\(i)] = \(values[i]!)")
      }
  }
  
  printArray(values)
  </code></pre>
  
  <p>Which will print the desired:</p>
  
  <pre><code>value[0] = 1
  value[1] = 2
  value[2] = foo
  value[3] = bar
  </code></pre>
  
  <p>But would still like to see how we can unwrap an Optional from <code>Any</code> as this is what <code>MirrorType.value</code> returns making it difficult to extract the Optional value, e.g:</p>
  
  <pre><code>class Person {
      var id:Int = 1
      var name:String?
  }
  
  var person = Person()
  person.name = "foo"
  
  var mt:MirrorType = reflect(person)
  for i in 0 ..&lt; mt.count {
      let (name, pt) = mt[i]
      println("\(name) = \(pt.value)")
  }
  </code></pre>
  
  <p>Prints out:</p>
  
  <pre><code>id = 1
  name = Optional("foo")
  </code></pre>
  
  <p>When I need:</p>
  
  <pre><code>id = 1
  name = foo
  </code></pre>