28

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?

0

4 Answers 4

21

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.

7
  • 1
    so Dictionary + NSDictionary is the same? but i see there are two ways to declare it. So i confused.
    – TomSawyer
    Aug 28, 2014 at 17:08
  • They're "the same" insofar as the runtime will convert between the two.
    – CodaFi
    Aug 28, 2014 at 22:03
  • 18
    Actually, Dictionary is a struct, not a class.
    – newacct
    Aug 28, 2014 at 22:53
  • 5
    I would also add that NSDictionary is a reference type where as Dictionary, like every struct in Swift, is a value type.
    – agy
    Aug 30, 2015 at 10:50
  • 1
    @Alston If you are writing Swift code, use a Dictionary, the Swift type. If you are writing (or talking to) Objective-C code, use (or cast to) NSDictionary.
    – matt
    Oct 27, 2016 at 13:45
7

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.

2

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.

1
  • 4
    Are they though? Swift dictionaries are pass by value, whereas NSDictionaries are pass by reference. May 17, 2016 at 17:17
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.