将NSDictionary转换为Swift Dictionary

现在我知道,当swift编译它只是一个NSDictionary,但NSDictionary和Swift词典有不同的语法。 有没有一种方法(通过循环或其他)将NSDictionary转换为相同types的快捷字典<key, value>

要么

有没有办法将其转换为Swift字典而不是NSDictionary?

 let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary 

使用:

 let jsonDic = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary<String, AnyObject>; 

我从http://www.swift-studies.com/blog/2014/6/6/loading-a-swift-dictionary-from-a-plist-filefind答案;

 var swiftDict : Dictionary<String,AnyObject!> = Dictionary<String,AnyObject!>() for key : AnyObject in ocDictionary.allKeys { let stringKey = key as String if let keyValue = ocDictionary.valueForKey(stringKey){ swiftDict[stringKey] = keyValue } } 

NSDictionaryDictionary几乎可以互换。 所以没有必要,但是你可以:

 let jsonDict = (NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary) as Dictionary 

只要声明它是Swift字典

 var values: [String: AnyObject] = [ "key1": 100, "key2": "meow", ] values["key3"] = 20