字典得定义
//创建空字典
var empty:[String:String] = [:]
var emptytow:Dictionary<String,String> = [:]
var emptythree = String:String
//字典得创建
var dicone = ["name":"张三","sex":"男"]
var dictwo:[String:String] = ["name":"赵四","sex":"男"]
var dicthree:Dictionary<String,String> = ["name":"曲中论","sex":"男","hooby":"computer"]
字典得一些基本操作,长度判断是否为空,取值。
print(dicthree.count)
print(dicone.isEmpty)
print(dicthree["name"]!)
//字典得特点:1.无序2.键不能重复
let x = ["a":"1","b":"2","c":"3"]
let y = ["c":"3","b":"2","a":"1"]
print(x==y)
//let c = ["a":"1","b":"2","c":"3","c":"4"]--报错
遍历 Key value 使用得keys values
for k in dicthree.keys{
print(k)
}
for y in dicthree.values{
print(y)
}
for (k,y) in dicthree{
print(k,y)
}
增删改查
var word = ["name":"亭","age":"21","sex":"男"]
//增方法1
word["hobby"]="xiha"
print(word)
//增方法2
word.updateValue("大美",forKey:"蚌埠")
print(word)
//删除方法1
word.removeValue(forKey:"蚌埠")
print(word)
//删除方法2
word["hobby"] = nil
print(word)
//改1
word["name"] = "小亭"
print(word)
//改2
var Ex = word.updateValue("18",forKey:"age")!
if Ex == word["age"]{
print("修改后得已修改前得一样")
}
print(word)