Convert NSDictionary to Swift Dictionary

后端 未结 3 1038
野的像风
野的像风 2020-12-14 05:48

Now I know that when swift compiles it just makes a NSDictionary, but the NSDictionary and Swift dictionaries have different syntax. Is there a way (through a loop or somet

相关标签:
3条回答
  • 2020-12-14 06:27

    NSDictionary and Dictionary are pretty much interchangeable. So there's no need to, but yes you can:

    let jsonDict = (NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary) as Dictionary
    
    0 讨论(0)
  • 2020-12-14 06:32

    I found answer from http://www.swift-studies.com/blog/2014/6/6/loading-a-swift-dictionary-from-a-plist-file

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-14 06:41

    use:

    let jsonDic = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary<String, AnyObject>;
    
    0 讨论(0)
提交回复
热议问题