swift-字典
2019-05-10 本文已影响0人
当优秀成为习惯
字典
/// 定义并实例化字典
var dict = [String: AnyObject]()
dict["name"] = "zhangsan"
dict["age"] = 18
print(dict)
// 设置相同 key,之前的数值会被覆盖
dict["name"] = "lisi"
print(dict)
// 删除某一个 key
dict.removeValueForKey("age")
print(dict)
dict["title"] = "manager"
print(dict)
// 遍历字典(k, v可以随便写)
for (k, v) in dict {
print("\(k) -- \(v)")
}
// 合并字典
var dict2 = ["name": "wangwu", "age": 80, "title": "boss"]
for (k, v) in dict2 {
dict.updateValue(v, forKey: k)
}
print(dict)