title: Swift: call methods of an optional variable
body: <p>So I know the difference between '!' and '?', I just want to know what is the best way to use them when calling a method of an optional variable</p>
  
  <pre><code>var bar: Bar? = nil
  
  bar?.doSomething()  // this will be valid, but wouldn't call doSomething
  bar!.doSomething() // Given error: EXC_BAD_INSTRUCTIONS (Obviously)
  </code></pre>
  
  <p>But when 'bar' is not nil, these two method-calls are both valid.</p>
  
  <pre><code>bar = Bar()
  bar?.doSomething()  // Valid
  bar!.doSomething()  // Valid
  </code></pre>
  
  <p>So my question is, what is the best way to call methods of an optional variable, I personally use:</p>
  
  <pre><code>if bar != nil {
      bar!.doSomething()
  }
  </code></pre>
  
  <p>or will <strong>bar?.doSomething()</strong> do exactly the same?</p>