What is difference between NSDictionary vs Dictionary in Swift?

萝らか妹 提交于 2019-12-09 14:04:08

问题


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 difference between these two?
I want to use an array with index in Swift like an array in PHP.
Which one is better to use?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/25554259/what-is-difference-between-nsdictionary-vs-dictionary-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!