What is difference between NSDictionary vs Dictionary in Swift?

后端 未结 4 1910
清歌不尽
清歌不尽 2021-02-18 12:52

I\'m learning Swift, and I can see Dictionary in it.
But there are lots of examples that are using NSDictionary with Swift.
What\'s the

相关标签:
4条回答
  • 2021-02-18 13:23

    Here's the deal,

    For all intents and purposes, they are the same thing. So long as you import Foundation, the compiler will know them as the same thing.

    As for specific differences, thus Apple doc helps.

    Overview

    NSObject is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

    What that means, in a nutshell, is that NSObjects are ancient relics from olden (objective c) times.

    As for what is better, that is up to you. I find swift objects better than the ns counterpart just for the purpose of keeping code modern, however you may have to use ns objects if you are using code like NSURLCONNECTION that requires ns objects.

    Hope this helps.

    0 讨论(0)
  • 2021-02-18 13:28

    Dictionary is a native Swift struct. NSDictionary is a Cocoa class. They are bridged to one another (within the usual limits), as the docs explain very clearly and fully.

    It's exactly parallel to Array and NSArray.

    0 讨论(0)
  • 2021-02-18 13:29

    Consider this example

        let dataArray = NSMutableArray()
    
        let d0 = ["code":"AA","name":"American Airlines"]
        let d1 = ["code":"BA","name":"British Airlines"]
        let d2 = ["code":"DA","name":"Delta Airlines"]
    
        dataArray.addObject(d0)
        dataArray.addObject(d1)
        dataArray.addObject(d2)
    

    later...

    let d0 = dataArray.objectAtIndex(0) as! [String:String]
    let lbl = UILabel() 
    lbl.text = d0["code"] // no xcode warnings
    

    Using NSDictionary in swift

    let d0 = dataArray.objectAtIndex(0) as! NSDictionary 
    

    Now you need to coerce the dictionary value into shape.

    lbl1.text = d0["name"] as! String
    

    From experience - when dealing with json responses - I've used NSDictionary as it's loosely typed and I can't guaranteed to know exactly what it contains - just that is valid json. There's a growing library of native swift tools to handle this use case, I suggest looking here https://github.com/search?q=json+NSDictionary+swift&type=Code&utf8=%E2%9C%93

    In this case - you need to hack around data with the 'if let' dance.

    if let results = json["result"] as? NSDictionary
    

    lastly - NSMutableDictionary is more on par with Dictionary as you won't be able to mutate the key / value pairs on a NSDictionary.

    eg.

    results["code"] = 100 // <-- BOOM
    

    the work around -

    let mResults = NSMutableDictionary(dictionary:results)
    
    mResults["code"] = NSNumber(100) // <-- OK
    

    which begs the clarification - you can only stick NSObjects (NSNumbers / NSArrays / NSCount/NSSet) inside the NSMutableDictionaries. The Swift Dictionary gets around this.

    0 讨论(0)
  • 2021-02-18 13:40

    In practice and with regard to Swift's strong type concept the significant difference is

    • NSDictionary is generally type unspecified
    • Dictionary is supposed to have a specific type.

    Hence native Dictionary is preferable.

    0 讨论(0)
提交回复
热议问题