I have a .json file that I am serializing into a Swift dictionary.
typealias Dict = Dictionary<String,AnyObject>
func loadDictionaryFromJSON(filePath:String) -> Dict
{
var JSONData:NSData! = NSData.dataWithContentsOfMappedFile(filePath) as NSData
var JSONError:NSError?
let swiftObject:AnyObject = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.AllowFragments, error: &JSONError)!
if let nsDictionaryObject = swiftObject as? NSDictionary
{
if let dictionaryObject = nsDictionaryObject as Dictionary?
{
return dictionaryObject as Dict
}else
{
println("Error could not make dictionary from NSDictionary in \(self)")
}
}else
{
"Error could not make NSDictionary in \(self)"
}
println("Empty dictionary passed, fix it!")
return Dict()
}
However, I am having trouble getting the objects this now. My .json is basically a dictionary of dictionaries (with various levels of nesting). So to start I am grabbing each object in the top level (which are all dictionaries).
for object in objects
{
var dict:Dictionary<String,AnyObject> = object
}
However, the above line throws the error
(key: AnyObject, value: AnyObject)' is not convertible to 'Dictionary<String, AnyObject>
How can I properly cast each object in my Dictionary to a Dictionary <String,AnyObject>
?