title: Swift 3.0 Optional Chaining
body: <p>I am new to swift so this is definitely a newbie level question. I was pondering on some Swift 3.0 documentation where I noticed a potential error. I am wondering if the example is incorrect (or ambiguous) or I am in fact missing out on some guideline.</p>
  
  <p>See <a href="http://swiftdoc.org/v3.0/type/Optional/" rel="nofollow">http://swiftdoc.org/v3.0/type/Optional/</a> the section about Optional Chaining.</p>
  
  <blockquote>
    <p><strong>OPTIONAL CHAINING</strong></p>
    
    <p>To safely access the properties and methods of a wrapped instance, use the postfix optional chaining operator (<code>?</code>). The following example uses optional chaining to access the <code>hasSuffix(_:)</code> method on a <code>String?</code> instance.</p>
  
  <pre><code>if let isPNG = imagePaths["star"]?.hasSuffix(".png") {
      print("The star image is in PNG format")
  }
  // Prints "The star image is in PNG format"
  </code></pre>
  </blockquote>
  
  <p>AFAIU, <code>imagePaths["star"]?.hasSuffix(".png")</code> is only supposed to safely unwrap <code>imagePaths</code> and run <code>hasSuffix()</code> only if <code>imagePaths["star"]</code> results in <code>Optional.some(wrapped)</code>. This implies that <code>isPNG</code> will be either <code>true</code> or <code>false</code>. Therefore, the implication of the above sample is incorrect where it implicitly claims that if this safely unwraps, then the value is always true.</p>
  
  <p>Here are some examples to explain what I am talking about:</p>
  
  <pre><code>if let isPNG = imagePaths["star"]?.hasSuffix(".png") {
      print("The star has png format")
  } else {
      print("The star does not have png format")
  }
  
  if let isPNG = imagePaths["portrait"]?.hasSuffix(".png") {
      print("The portrait has png format")
  } else {
      print("The portrait does not have png format")
  }
  // "The portrait has png format\n"
  
  if let isPNG = imagePaths["alpha"]?.hasSuffix(".png") {
      print("The alpha has png format")
  } else {
      print("The alpha does not have png format")
  }
  // "The alpha does not have png format\n"
  </code></pre>
  
  <p>I am simply wondering if my current analysis is wrong or if SwiftDoc.org needs to change this particular example.</p>