I'm relatively new to coding iOS and have not fully wrapped my head around optionals, downcasting, dictionaries and related fun concepts. I would greatly appreciate help on the following.
I am downloading data from a database and want to perform checks on the data to avoid a crash. In this particular case I want to check if an Object in a dictionary is an Int before performing a task to avoid a crash.
//The downloaded dictionary includes Int, Double and String data
var dictionaryDownloaded:[NSDictionary] = [NSDictionary]()
//Other code for downloading the data into the dictionary not shown.
for index in 1...dictionaryDownloaded.count {
let jsonDictionary:NSDictionary = self.dictionaryDownloaded[index]
if (jsonDictionary["SUNDAY OPEN TIME"] as? [Int]) != nil {
self.currentlyConstructingRecommendation.sundayOpenTime = jsonDictionary["SUNDAY OPEN TIME"] as! Int!
}
self.recommendationsArray.append(currentlyConstructingRecommendation)
}
I followed the approach from this related question and answer. However, the problem is the "if (jsonDictionary["SUNDAY OPEN TIME"] as? [Int]) != nil" line is never true. I believe that is because the value is an optional object. I tried adjusting the dictionary to be of type [String:AnyObject] but that did not have an impact.
I'm stuck and any ideas you have would be appreciated. Please let me know if there is any more detail that is helpful. Thanks!