1.字典的定义:
func demo(){
let dic = ["name":"zhufeng","age":200]
print(dic)
/定义字典的数值 用的比较多 ,key 都是String类型的 一定要保证这一点了操作了
let dic2 :[[String:AnyObejct]]={
["name":"zhufeng","age":20],
["name":"zhfueng2","age":30]
}
}
2.字典的遍历:
func demo(){
print("字典的遍历方法---------")
letarray = ["name":"zhufeng","age":17]as[String:Any]
//使用for来循环遍历出来的东西
for e in array {
print("\(e.key):\(e.value)")
}
for(key ,value)inarray{
print("\(key):\(value)")
}
}
}
3.字典的增删改:
func demo(){
print("字典的增,删,改的东西------")
vardic = ["name":"zhufeng","age":18]as[String:Any];
print(dic)
//新增东西,如果key不存在,就是添加了
dic["title"] ="测试下"
print(dic)
//改变,如果是key存在就是修改
dic["name"] ="zhufeng2"
print(dic)
//删除通过key来删除
//科普通过key来定位,key,key必须是hash值
dic.removeValue(forKey:"age")
print(dic)
}
4.字典的合并:
func demo(){
vararray= ["name":"zhufeng","age":18]as[String:Any]
vararray2 = ["name":"zhufeng2","age":20,"title":"老王"]as[String:Any]
for(key ,value)inarray2{
array[key] = array2[key]
}
print(array) // ["name": "zhufeng2", "age": 20, "title": "老王"]
}