title: Swift cast dictionary value as type
body: <p>I am looking for best way how can I make this code to one line:</p>
  
  <pre><code>if (dictionary["Amoumt"] is Double) {
      amount = dictionary["Amount"] as Double
  } else {
      amount = NSString(string: dictionary["Amount"] as String).doubleValue
  }
  </code></pre>
  
  <p>I have <code>Dictionary&lt;String, AnyObject&gt;</code> and I would like to parse values from it. I am using code like above but it's too many lines. I would like to make it to one line. Something like:</p>
  
  <pre><code>dictionary["Amount"].parseDouble()
  </code></pre>
  
  <p>There is no problem to create extension with this method:</p>
  
  <pre><code>func parseDouble() -&gt; Double {
      if (self is Double) {
          return self as Double
      } else {
          return NSString(string:(self as String)).doubleValue
      }
  }
  </code></pre>
  
  <p>But which type should I extend? Next could you help me with generic method? So I could call something like this:</p>
  
  <pre><code>dictionary["Amount"].parse(Double)
  </code></pre>
  
  <p>And is this good way how to do this or should I do it another way?</p>