//1.定義不可變字典:使用let修飾
let dict :[String : Any] = ["name":"rae","nikeName":["hand","some","cool","..."]]
let dict : Dictionary<String,Any> = ["name":"rae","nikeName":["hand","some","cool","..."]]
let arr = dict["nikeName"]
//2.定義可變字典:使用var修飾
var dict = [String : Any]()
var dict = Dictionary<String,Any>()
//添加元素
dict["name"] = "why"
dict["nikeName"] = ["aaa","bbb","ccc"]
dict["age"] = 18
//刪除元素
dict.removeAll()
dict.removeValue(forKey: "age")
//修改元素
dict["name"] = "jjjj"
dict.updateValue("kkk", forKey: "name")
//3.遍歷字典
//1.遍歷字典中所有的key
for key in dict.keys {
print(key)
}
//2.遍歷字典中所有的value
for value in dict.values {
print(value)
}
//3.遍歷字典中所有的key/value
for (key,value) in dict {
print(key,value)
}
//4.字典合并
var dict1 : [String :Any] = ["name":"why","age":10]
let dict2 : [String :Any] = ["height":1.83,"phone":"+86"]
for (key,value) in dict2 {
dict1[key] = value
}