title: Safely down casting from Int to Int8 in Swift
body: <p>I receive a set of integers via an api json response (which I convert into a <code>[String: Any]</code> dictionary). These integers are guaranteed to be in the range-10...-10 (inclusively). In my model I'd like to store these as <code>Int8</code>.</p>
  
  <p>This is how I'm converting my json dictionary to it's model object but I'm wondering if there is a more idiomatic way of ensuring the ints in the json actually fit in an <code>Int8</code>?</p>
  
  <pre><code>func move(with dict: JSONDictionary) -&gt; Move? {
      if let rowOffset = dict[JSONKeys.rowOffsetKey] as? Int,
         let colOffset = dict[JSONKeys.colOffsetKey] as? Int {
  
        if let rowInt8 = Int8(exactly: rowOffset),
           let colInt8 = Int8(exactly: colOffset) {
  
          return Move(rowOffset: rowInt8, colOffset: colInt8)
        }
        else {
          print("Error: values out of range: (row: \(rowOffset), col: \(colOffset)")
        }
      } else {
        print("Error: Missing key, either: \(JSONKeys.rowOffsetKey) or \(JSONKeys.colOffsetKey)")
      }
  
      return nil
    }
  </code></pre>
  
  <p>Note that doing the following always fails regardless of the value of the incoming ints:</p>
  
  <pre><code>if let rowOffset = dict[JSONKeys.rowOffsetKey] as? Int8,
     let colOffset = dict[JSONKeys.colOffsetKey] as? Int8 {
  ...
  </code></pre>
  
  <p>This is how I'm converting the incoming json to a dictionary. The json in question is deeply nested and contains several different types.</p>
  
  <pre><code>typealias JSONDictionary = [String: Any]
  let jsonDict = try JSONSerialization.jsonObject(with: data) as? JSONDictionary
  </code></pre>