I am trying to parse JSON data to a dictionary, for parsing I am using the separate method, and later would like to use the results (dictionary) for other operations in another method, not just to print it out as it is given in many examples online, e. g. here.
However, I cannot return the value since I was asked to insert return statement inside guard, but after the insertion getting "Non-void function should return a value".
The code looks the following way:
func extractJSONDictionaryFrom(JSONData:NSData) ->NSMutableDictionary
{
var dict = NSMutableDictionary()
do {
guard let JSON = try NSJSONSerialization.JSONObjectWithData(JSONData, options:NSJSONReadingOptions(rawValue: 0)) as? NSDictionary else {
print("Not a Dictionary")
return
}
dict = NSMutableDictionary(dictionary: JSON)
}
catch let JSONError as NSError {
print("\(JSONError)")
}
print("The JSON is \(dict)")
return dict
}
The approach using throw is as well hardly useful since I need to handle throws in other methods when calling "extractJSONDictionaryFrom"