TomSawyer
TomSawyer

Reputation: 3820

What is difference between NSDictionary vs Dictionary in Swift?

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?

Upvotes: 28

Views: 18638

Answers (4)

rocket101
rocket101

Reputation: 7537

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.

Upvotes: 2

johndpope
johndpope

Reputation: 5249

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.

Upvotes: 1

vadian
vadian

Reputation: 285082

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.

Upvotes: 7

matt
matt

Reputation: 535315

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.

Upvotes: 21

Related Questions