1

I want to add multiple values in same key of dictionary. I have a Dic of type

 var dic1 = [String:[String:AnyObject]](

 dic1.updateValue(["name": cellName,"phone": Phone], forKey: "XYZ")

By this its updating value of key "XYZ" but I want to add more values in same key "XYZ". Can anyone help me out in this?

Dravidian
  • 9,945
  • 3
  • 34
  • 74
Navjot Singh
  • 65
  • 12
  • you need to add that custom operator to use his += suggestion `func += (inout lhs: [Key: Value], rhs: [Key: Value]) { rhs.forEach{ lhs[$0] = $1 } }` – Leo Dabus Aug 30 '16 at 18:55
  • @LeoDabus Can you please help me in this. May be by steps or whatsoever suitable for you – Navjot Singh Aug 30 '16 at 18:57
  • This will solve the error += cannot be used with two [String:AnyObject] operands – Leo Dabus Aug 30 '16 at 18:58
  • @LeoDabus but I don't you how to use your code. please help me out . – Navjot Singh Aug 30 '16 at 18:59
  • just put that at the top level of any of your Swift files in your project – Leo Dabus Aug 30 '16 at 19:00
  • http://stackoverflow.com/a/35740240/2303865 – Leo Dabus Aug 30 '16 at 19:02
  • @LeoDabus I did what you said but its still updating the values for key "XYZ" instead of adding – Navjot Singh Aug 30 '16 at 19:07
  • Please add the actual code with the actual result and the expected one – Leo Dabus Aug 30 '16 at 19:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122229/discussion-between-navjot-singh-and-leo-dabus). – Navjot Singh Aug 30 '16 at 19:10
  • @LeoDabus I know It increases your efforts but can you please join the chat – Navjot Singh Aug 30 '16 at 19:18
  • I cant help you further if you don't illustrate your problem. Once you have updated your question please let me know – Leo Dabus Aug 30 '16 at 19:19
  • @LeoDabus i illustrated everything in chat including coding,output and expected output thats why requesting you to join chat – Navjot Singh Aug 30 '16 at 19:21
  • You are updating the whole dictionary and you are supposed to update using the key – Leo Dabus Aug 30 '16 at 19:23
  • It is not easy to visualize the code inside the chat – Leo Dabus Aug 30 '16 at 19:24
  • Try dict["XYZ"] += theNewDictionar – Leo Dabus Aug 30 '16 at 19:26
  • 1
    @NavjotSingh when answers are satisfactory to what you want, given the amount of information that you provide, all you need to do is accept them and upvote.You also need to understand SO is not a personal help center, mostly why people answer here is to help other people who might end up with your problem, Thats why we try to generalise our answers as much as possible or if you want to be answered specifically then provide with your specific details. Please look up:- http://stackoverflow.com/help/someone-answers. – Dravidian Aug 31 '16 at 07:09

4 Answers4

1

A simple and elegant solution:-

var mutDictionary = [String: NSMutableDictionary]()

//Initialising your mutDictionary:-
mutDictionary = ["Fighters" : ["First" : ["NAME" : "The Tigress", "Phone" : "2131231231"]]]

//Adding a key-hashabale(or in this case String-NSMutableDictionary) pair.  
//`.updateValue` adds a new parent pair to your mutDictionary if your key value is not present if the key is present then it will update that key's value.
mutDictionary.updateValue(["First" : ["NAME" : "Uruguay", "Phone" : "903192301293"]], forKey: "The Bosses")
mutDictionary.updateValue(["FirstOfHisKind" : ["NAME" : "Panda", "Phone" : "123454362"]], forKey: "The Dragon Warrior")    

//Appending data to your mutDictionary:-
mutDictionary["Fighters"]?.setObject(["NAME" : "Guru Shifu", "Phone" : "121212121212"], forKey: "Second")

 print(mutDictionary)

The output that you get from this is something like this :-

   ["Fighters"          : { Second = {  NAME = Guru Shifu; 
                                        Phone = 121212121212;};
                            First = {  NAME = The Tigress;  
                                       Phone = 2131231231;};
                              },
    "The Bosses"        : {First = { NAME = Uruguay;
                                     Phone = 903192301293;};
                              },
    "The Dragon Warrior": {First = { NAME = Panda;
                                     Phone = 123454362;};
                            }]
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Thanks a lot buddy. Its not exactly what I want but gave me an idea and I got the desirable output. I am uploading it as a answer – Navjot Singh Aug 31 '16 at 05:30
  • @NavjotSingh when answers are satisfactory to what you want, given the amount of information that you provide, all you need to do is accept them and upvote.You also need to understand SO is not a personal help center, mostly why people answer here is to help other people who might end up with your problem, Thats why we try to generalise our answers as much as possible or if you want to be answered specifically then provide with your specific details, which you didn't. Please look up:- http://stackoverflow.com/help/someone-answers. – Dravidian Aug 31 '16 at 07:09
  • can you help me in sorting this array "mutDictionary" by key "name" – Navjot Singh Aug 31 '16 at 07:26
  • This is a different question entirely. Avoid building up questions in comments, search net for them, if you dont find any solution ask a well asked question on SO. – Dravidian Aug 31 '16 at 07:36
0

Do you mean like this?

var value = Dict1["XYZ"]
value["address"] = "AAA BBB CCC 1234"
Dict1["XYZ"] = value
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
0

If I have understood the question:

1) you have to define a class and you can put many values inside it. Example:

 var user_location, user_mail, user_name, user_pass, user_urlPicture: String!

init(user_location: String!, user_mail: String!, user_name: String!, user_pass: String!, user_urlPicture: String!){

    self.user_location = user_location
    self.user_mail = user_mail
    self.user_name = user_name
    self.user_pass = user_pass
    self.user_urlPicture = user_urlPicture

}
// DictionaryConvertible protocol methods
required convenience init?(dict: [String:AnyObject]) {
    guard let user_location = dict["user_location"] as? String, user_mail = dict["user_mail"] as? String, user_name = dict["user_name"] as? String, user_pass = dict["user_pass"] as? String, user_urlPicture = dict["user_urlPicture"] as? String else {
        return nil
    }
    self.init(user_location: user_location, user_mail: user_mail, user_name: user_name, user_pass: user_pass, user_urlPicture: user_urlPicture)
}
var dict:[String:AnyObject] {
    return [
        "user_location": user_location,
        "user_mail": user_mail,
        "user_name": user_name,
        "user_pass": user_pass,
        "user_urlPicture": user_urlPicture
    ]
}

2) In your ("User") class you can add your values:

let user = User(yourLocation, user_mail: yourMail, user_name: yourName, user_pass: yourPass, user_urlPicture: yourUrlPicture)
Carlo
  • 813
  • 1
  • 15
  • 34
0
        var mutDictionary = [String: NSMutableDictionary]()
        let keyVar = String(cellName[cellName.startIndex])
        if mutDictionary[keyVar] == nil {
          mutDictionary.updateValue([:], forKey: keyVar)
          mutDictionary[keyVar]?.setObject(["Name" : cellName, "Phone" : Phone], forKey: cellName)
        }
        else{
          mutDictionary[keyVar]?.setObject(["Name" : cellName, "Phone" : Phone], forKey: cellName)
        }
    print(mutDictionary)
Navjot Singh
  • 65
  • 12