var dict:Dictionary<String, String> = ["swift":"快速", "java":"爪哇岛"]
var emptyDictionary1:[String:Int] = [:]
var emptyDictionary2:Dictionary<Int,String> = [:]
var emptyDictionary3 = [String:String]()
var emptyDictionary4 = Dictionary<Int,Int>()
print(dict["swift"])
dict["C++"]
if let value = dict["swift"]{
print("swift 的意思是 - \(value)")
}
dict.count
dict.isEmpty
emptyDictionary1.isEmpty
Array(dict.keys)
Array(dict.values)
for key in dict.keys{
print("key")
}
for value in dict.values{
print(value)
}
for (key, value) in dict{
print("\(key):\(value)")
}
let dict1 = [1:"A",2:"B",3:"C"]
let dict2 = [1:"A",2:"B",3:"C"]
dict1 == dict2
var user = ["name":"bobobo", "password":"123456", "occupation":"programmer"]
user["occuption"] = "freelancer"
user.updateValue("imooc", forKey: "password")
let oldPassword = user.updateValue("imooc", forKey: "password")
if let oldPassword = oldPassword, newPassword = user["password"] where oldPassword == newPassword{
print("注意:修改后的密码和之前一样,可能导致安全问题")
}
//增
user["email"] = "imooc@imooc.com"
user.updateValue("imooc.com", forKey: "website")
//删
user["website"] = nil
//user.removeValueForKey("email")//或者先进行解包,
if let email = user.removeValueForKey("email"){
print("电子邮箱\(email)删除成功")
}