字典的定义和增删改
var dict: Dictionary = [String: Any]()
dict["name"] = "Jerry"
dict["age"] = 18
print(dict)
dict["age"] = 26
dict["height"] = 1.80
print(dict)
dict.removeValue(forKey: "height")
print(dict)
dict.removeAll()
print(dict)
字典的遍历和合并
let dict = ["name" : "Jerry", "age" : "18", "height" : "185cm"]
for d in dict {
print("key = \(d.key), value = \(d.value)")
}
for (key, value) in dict {
print("key: \(key), value: \(value)")
}
var dict2 = ["haha" : "哈哈"];
for d in dict {
dict2[d.key] = dict[d.key]
}
print(dict2)