Dictionary

一,简介 【Dictionary】字典是一种存储相同类型多重数据的存储器。每个值(value)都关联独特的键(key),键作为字典中的这个值数据的标识符。和数组中的数据项不同,字典中的数据项并没有具体顺序。我们在需要通过标识符()访问数据的时候使用字典。
二,字典初始化

var dict = ["name":"sunfusheng", "age":20, "blog":"sunfusheng.com"]var dict1:Dictionary = [:] var dict2:[String:String] = [:]var dict4 = Dictionary() var dict3 = [String:String]()

三,增删改查
dict[key] = value dict.updateValue(value, forKey: key)dict.removeValueForKey(key") dict.removeAll()

四,遍历
// key集合 print(Array(dict.keys)) // value集合 print(Array(dict.values))// 遍历字典的键 for key in dict.keys { print(key) } // 遍历字典的值 for value in dict.values { print(value) } // 遍历key,value,无序 for (key, value) in dict { print("\(key):\(value)") }// 遍历elements for elements in dict//无序 { println("键:\(elements.0) 值:\(elements.1)"); }

    推荐阅读