I have a dictionary coming from the parsing of a JSON (coming from an outside source). I want to store the values that are present in fixed properties, something similar to:
func feedJSONdata(jsonDict: NSDictionary) {
var1 = jsonDict["var1"] as! String
var2 = jsonDict["var2"] as! String
var3 = jsonDict["var3"] as! String
var4 = jsonDict["var4"] as! String
var5 = jsonDict["var5"] as! String
// and so on
}
Is there a way to guard against any of the entries in the dictionary to be missing?
Repeating:
if let var1Entry = jsonDict["var1"] as? String {
var1 = var1Entry
}
seems heavy handed to me. Any lighter option?
Second, I may also want to guard against the dictionary entry being present but not a string.
Thanks,
Jean-Denis