I am searching a key in an Array
of Dictionarys
and I want to convert the result value into an Int
value. This is what I tried.
If your data is decoded from JSON string, it will be decoded as NSNumber or NSString.
You can use this function:
func intValueForKey(key: String, inDictionary dictionary: [String: AnyObject]) throws -> Int {
if let value = dictionary[key]?.integerValue {
return value
} else {
throw NSError(domain: "Invalid key", code: -1, userInfo: nil)
}
}