我在swift中有字典
var data = ["GenInfo":Dictionary<String,String>(),"LangInfo":Array<String>(),"EduInfo":Array<Dictionary<String,String>>(),"JobInfo":Array<Dictionary<String,String>>(),"SkillInfo":Array<Dictionary<String,String>>()]
现在我想为这个字典添加值,我该怎么做。 假设我想添加这些
"FirstName": "Varun",
"Email": "varun@gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
valueForKey "GenInfo"
答案 0 :(得分:1)
你在哪里制作一个字典(即NSDictionary),它的声明后不可变(与NSMutableDictionary不同)。
那就是说,你可以这样做:
var data : NSMutableDictionary = ["GenInfo":Dictionary<String,String>(),"LangInfo":Array<String>(),"EduInfo":Array<Dictionary<String,String>>(),"JobInfo":Array<Dictionary<String,String>>(),"SkillInfo":Array<Dictionary<String,String>>()]
data["GenInfo"] = ["FirstName": "Varun",
"Email": "varun@gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
]
data["LangInfo"] = ["English", "French", "Italian"]
data["EduInfo"] = [["Degree": "MCA", "School": "University of Kota", "Year": "2013"], ["Degree": "Another degree", "School": "University of London", "Year": "2015"]]
// And so on...
或者像这样:
var data = [String: AnyObject]()
data["GenInfo"] = [String: String]() // Dictionary<String,String>()
data["LangInfo"] = [String]() // Array<String>()
data["EduInfo"] = [[String: String]]() // Array<Dictionary<String,String>>()
data["JobInfo"] = [[String: String]]() // Array<Dictionary<String,String>>()
data["SkillInfo"] = [[String: String]]() // Array<Dictionary<String,String>>()
data["GenInfo"] = ["FirstName": "Varun",
"Email": "varun@gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
]
data["LangInfo"] = ["English", "French", "Italian"]
data["EduInfo"] = [["Degree": "MCA", "School": "University of Kota", "Year": "2013"], ["Degree": "Another degree", "School": "University of London", "Year": "2015"]]
// And so on...
答案 1 :(得分:0)
喜欢这个
var data: NSMutableDictionary = ["GenInfo":Dictionary<String,String>(),"LangInfo":Array<String>(),"EduInfo":Array<Dictionary<String,String>>(),"JobInfo":Array<Dictionary<String,String>>(),"SkillInfo":Array<Dictionary<String,String>>()]
data["GenInfo"] = ["FirstName": "Varun",
"Email": "varun@gmail.com",
"State": "Rajasthan",
"Address": "Plot No. 00, Bhagwan Nagar 31,",
"Zip": "21354",
"Phone": "123456789",
"LastName": "Sharma"
]