Dimuth
Dimuth

Reputation: 750

Copy of NSDictionary to NSMutableDictionary

I need to know that how can i make a copy of NSDictionary to NSMutableDictionary and change values of there.

Edit

I ned to know how to modify data of a NSDictionary. I got to know that

Copy data of NSDictionary to a NSMutableDictionary. and then modify data in NSMutableDictionary

Upvotes: 5

Views: 12020

Answers (2)

Apple-and-Oranges
Apple-and-Oranges

Reputation: 498

let f : NSDictionary = NSDictionary() 
var g = f.mutableCopy()

Upvotes: 17

Fred Faust
Fred Faust

Reputation: 6800

You should initialize the NSMutableDictionary using it's dictionary initializer, here's a quick example in Playground

let myDict:NSDictionary = ["a":1,"b":2]

let myMutableDict: NSMutableDictionary = NSMutableDictionary(dictionary: myDict)

myMutableDict["c"] = 3

myMutableDict["a"] // 1
myMutableDict["b"] // 2
myMutableDict["c"] // 3

Alternatively, you can declare a Swift dictionary as a var and mutate it whenever you want.

var swiftDictioanry : [String:AnyObject] = ["key":"value","key2":2]

The AnyObject value type mimics the behavior of an NSDictionary, if all types are known it can be declared like so:

var myNewSwiftDict : [String:String] = ["key":"value","nextKey":"nextValue"]

Upvotes: 10

Related Questions