gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:35:22 26 4
gpt4 key购买 nike

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

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

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

通过它更新键“XYZ”的值,但我想在同一个键“XYZ”中添加更多值。谁能帮我解决这个问题?

最佳答案

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

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

关于ios - 在具有字典作为“值”的字典中追加和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com