/// 定义并实例化字典
var dict = [String: AnyObject]()
dict["name"] = "zhangsan"
dict["age"] = 18
println(dict)
// 设置相同 key,之前的数值会被覆盖
dict["name"] = "lisi"
println(dict)
// 删除某一个 key
dict.removeValueForKey("age")
println(dict)
dict["title"] = "manager"
println(dict)
// 遍历字典(k, v可以随便写)
for (k, v) in dict {
println("\(k) -- \(v)")
}
// 合并字典
var dict2 = ["name": "wangwu", "age": 80, "title": "boss"]
for (k, v) in dict2 {
dict.updateValue(v, forKey: k)
}
println(dict)
时间: 2024-10-23 18:14:34