var dict = ["Swift":"雨燕沪摄,快速的", "Python":"大蟒", "Java":"爪哇島嚎研,咖啡", "Ruby":"鉆石"]
var emptyDictionary1: [String:Int] = [:]
var emptyDictionary2: Dictionary<String,Int> = [:]
var emptyDictionary3 = [String:Int]()
var emptyDictionary4 = Dictionary<String,Int>()
//遍歷鍵
for key in dict.keys {
print(key)
}
//遍歷鍵值
for(key,value) in dict {
print("\(key) : \(value)")
}
//字典比較
let a = [1:"A", 2:"B", 3:"C"]
let b = [1:"A", 3:"C", 2:"B"]
a == b // true
數(shù)組的增刪改查
var user = ["name":"zhenzhen", "password":"qinzhen", "occupation":"programmer"]
//增
user["e-mail"] = "imooc@imooc.com"
user.updateValue("imooc.com", forKey: "Website")
//刪
user["e-mail"] = nil //給鍵直接賦空值
user.removeValue(forKey: "password") //返回值是所刪除的值
if let email = user.removeValue(forKey: "password") {
print("電子郵箱 \(email) 已刪除成功")
}
//改
user["occupation"] = "freelancer"
user.updateValue("Imooc", forKey: "password") //返回值是所刪除的鍵對應(yīng)的值(舊值)
if let oldPassword = user.updateValue("Imooc", forKey: "password") ,
let newPassword = user["password"], oldPassword == newPassword {
print("警告:修改后的密碼跟之前密碼一樣闷畸!")
}