-2

I have NSDictionary, now I am trying to add an object in a particular key.How can I do that ? below is my code.

let businessPhoneDic = ["locationname" : "",
                                "area" : "",
                              "number" : "",
                                "type" : "",
                       ];
let emailDic:NSMutableDictionary? = ["email" : "",];

let businessPhoneDic2 = ["locationname" : "hello",
                                 "area" : "",
                          "phonenumber" : "",
                                 "type" : "",
                        ];

var mainDictionary = ["businessPhone" : businessPhoneDic as AnyObject,"email" : emailDic as AnyObject,
        ];

Now I want to add "businessPhoneDic2" into mainDictionary for key "businessPhone".How can I do that into Swift 3

vadian
  • 274,689
  • 30
  • 353
  • 361
A.T
  • 105
  • 3
  • 13
  • mainDictionary["businessPhone"] = businessPhoneDic2 – PGDev May 29 '17 at 09:35
  • Yes I was trying that but its just replace the old dictionary with the new value,So when I print mainDictionary["businessPhone"] after updating its replace businessPhoneDic with businessPhoneDic2. – A.T May 29 '17 at 09:38
  • Yes because a dictionary contains unique keys and only 1 value corresponding to that key. So you cannot store multiple values for a single key. – PGDev May 29 '17 at 09:39
  • Yes I want to append another value for with the previous dictionary – A.T May 29 '17 at 09:41
  • businessPhoneDic and businessPhoneDic2 contain same key-value pairs. So same logic, only unique key-value pairs are allowed. – PGDev May 29 '17 at 09:44
  • Can I merge businessPhoneDic and businessPhoneDic2 and after that I can update mainDictionary's "businessPhone" key. – A.T May 29 '17 at 09:49
  • What do you mean by merge? Like "locationname" : "" and "locationname" : "hello", which one do you want to keep? – PGDev May 29 '17 at 09:50
  • For adding elements of 1 dictionary into another refer to: https://stackoverflow.com/questions/24051904/how-do-you-add-a-dictionary-of-items-into-another-dictionary – PGDev May 29 '17 at 09:52

4 Answers4

1

By definition, you can't have two values mapped to the same key. The following code will replace the old key:

Swift 3

mainDictionary["businessPhone"] = businessPhoneDic2

Just use a new key if you need both values in your dictionary, or maybe store an array of dictionaries as values as such:

var mainDictionary : [String : [NSDictionary]] = ["businessPhone" : [businessPhoneDic]]

Then:

mainDictionary["businessPhone"]?.append(businessPhoneDic2)
Joe
  • 2,386
  • 1
  • 22
  • 33
  • I use this code var mainDictionary : [String : [NSDictionary]] = ["businessPhone" : [businessPhoneDic as NSDictionary]] mainDictionary["businessPhone"]?.append(businessPhoneDic2 as NSDictionary) print("=====", mainDictionary["businessName"] as AnyObject) but its return – A.T May 29 '17 at 09:45
  • Of course. You do not have a value for the key businessName in your mainDictionary. You should call mainDictionary[businessPhone].first!["businessName"] granted the businessPhoneDic dictionary has a businessName key – Joe May 29 '17 at 09:49
  • Ohh, I am sorry.Yes now its fine.I just want that Thanks – A.T May 29 '17 at 09:52
1

First of all do not use MSMutable... collection types in Swift.

Use native Swift Dictionary

let emailDic = ["email" : ""]

Second of all annotate a dictionary with different types as [String:Any]

var mainDictionary : [String:Any] = ["businessPhone" : businessPhoneDic, "email" : emailDic]

If the value for key businessPhone is an array you can append the value. If it's a single dictionary you have to create an array.

This code considers both cases:

let businessPhone = mainDictionary["businessPhone"]

if var phone = businessPhone as? [[String:Any]] {
    phone.append(businessPhoneDic2)
    mainDictionary["businessPhone"] = phone
} else if let phone = businessPhone as? [String:Any] {
    mainDictionary["businessPhone"] = [phone, businessPhoneDic2]
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Try this :

 mainDictionary["your key"] = businessPhoneDic2 as AnyObject // cast as per suggestions
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • why down voting ? – KKRocks May 29 '17 at 09:39
  • Yes I was trying that but its just replace the old dictionary with the new value,So when I print mainDictionary["businessPhone"] after updating its replace businessPhoneDic with businessPhoneDic2. – A.T May 29 '17 at 09:39
  • use different key because businessPhone key is already exist . – KKRocks May 29 '17 at 09:41
  • So how can I merge two dictionary and update it for that particular key? – A.T May 29 '17 at 09:42
  • you want to merge existing dictionary with new one ? – KKRocks May 29 '17 at 09:43
  • Can I merge businessPhoneDic and businessPhoneDic2 and after that I can update mainDictionary's "businessPhone" key. – A.T May 29 '17 at 09:47
  • you can't merge as dictionary as per your question. because there are all keys are same businessPhoneDic and businessPhoneDic2 if you set businessPhoneDic2 then it will be override ride with old dictionary. – KKRocks May 29 '17 at 09:50
  • you need to use array for "businessPhone". or add dictionary to that array. – KKRocks May 29 '17 at 09:51
0

Swift 3.x

// Initialize the Dictionary

var dict = ["name": "Foo", "surname": "Bar"]

// Add a new key with a value

dict["email"] = "foo.bar@email.com"
print(dict)
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Ashu
  • 3,373
  • 38
  • 34