1、创建字典:var dic:Dictionary<String,String> = ["学生": "sss",老师"ttt"];
2、添加新值:dic["门卫"] = "www"注:如果对应的key已经存在,则会更新原来的值
3、修改字典值:dic.updateValue("s2s",forKey: "学生") 注:如果不存在对应的key,则会作为新值添加到字典中。
4、获取某个key对应的value:dic["]
5、删除某个值dic.removeValueForKey(")
6、遍历字典:
获取所有的key
forkeyindic.keys{
print("key=:\(key)");
}
forvaluedic.values{
print("value=:\(value) }
遍历所有key和value
for tuples in dic {
print("key = \(tuples.0),value = \1)"
}
7、字典的赋值:
字典的赋值是深拷贝:
var dic2 = dic;
dic.removeValueForKey("学生")
print("dic2 = \(dic2)")
打印结果: dic2 = [" 学生 ": "sss"," 门卫 ": "w2w"," 老师 ": "ttt"]