在具有字典作为“值”的字典中追加和更新

时间:2016-08-30 18:19:24

标签: ios swift dictionary

我想在字典的同一个键中添加多个值。 我有一个类型的Dic

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

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

这是它的关键" XYZ"的更新值。但我想在同一个键中添加更多值" XYZ"。 任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

一个简单而优雅的解决方案: -

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)

你从中获得的输出是这样的: -

   ["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;};
                            }]

答案 1 :(得分:0)

你的意思是这样吗?

var value = Dict1["XYZ"]
value["address"] = "AAA BBB CCC 1234"
Dict1["XYZ"] = value

答案 2 :(得分:0)

如果我理解了这个问题:

1)你必须定义一个类,你可以在其中放入许多值。例如:

 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)在您的(" User")课程中,您可以添加您的值:

let user = User(yourLocation, user_mail: yourMail, user_name: yourName, user_pass: yourPass, user_urlPicture: yourUrlPicture)

答案 3 :(得分: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)