//创建空的字典
var dic = [Int:String]() //空字典 键是int型 值是string
dic[2] = "b" //key 2 对应的 value是b
dic = [:] //置空 不过类型还是 key是int value是string
//键值对创建
var dic2:[String:String] = ["key1":"value1","key2":"value2","key3":"value3"]
var dic3 = ["key1":"value1","key2":"value2","key3":"value3"]
var dic4:Dictionary<String,String> = ["key1":"value1"]
var dic5 = Dictionary<String,String>()
dic5 = dic4 //赋值
//获取元素数量
print(dic2.count)
//检查dic是否为空
if dic2.isEmpty {
}
//利用下标 修改元素 或者 添加元素
dic2["key1"] = "value11111"
dic2["key4"] = "value4"
//在键没有值的时候设置一个值,或者在键已经存在的时候更新它 并且在更新之后返回旧的值
let NewValue1 = dic3.updateValue("key1", forKey: "value123")
//取出值
let value3 = dic3["key3"];
//移除字典中的某一键值对
dic3["key3"] = nil
//根据key移除某一键值对 并返回移除的value
let removeValue = dic3.removeValue(forKey: "key1")
//字典的遍历
//每一个元素返回为 (key, value)元组
for (key , value) in dic2 {
print("(key):(value)")
}
//根据key的数组 value的数组 分别取出
for key in dic2.keys{
print("(key)的值是(dic2[key])")
}
for value in dic2.values{
print("值是(value)")
}
//获取 所有key 所有value
let KeyArray = [String](dic2.keys)
let ValueArray = [String](dic2.values)
//字典是无序的,以特定的顺序遍历字典的键或值,使用键或值的 sorted()方法
for key in dic2.keys.sorted()
{
}