Swift 字典

/*********************************************************
                    Swift 字典
*********************************************************/

var dictionary  = ["name":"LJF","age":"100"]
println(dictionary)
//1、字典键值对的添加
dictionary["height"] = "175"
println(dictionary)
//2、字典键值对的删除
dictionary.removeValueForKey("height")
println(dictionary)
//3、字典键值对的改动
//3.1使用键,改动固定键的相应值
dictionary["name"] = "TXM"
println(dictionary)
//3.2使用updateValue方法进行改动也能够加入键值对。当填入的键存在时就会对旧值的更新,不存在时返回nil  当键不存在的时候就会进行加入
let returnValue = dictionary.updateValue("99", forKey: "age")
let returnValue2 = dictionary.updateValue("99", forKey: "age1")
println("returnValue = (returnValue)","dictionary = (dictionary)")
println("returnValue = (returnValue2)","dictionary = (dictionary)")
//3.3字典键值对的查询(字典遍历)
for (key , value) in dictionary{
    println("key = (key)","value = (value)")
}
//4字典初始化的方式也有两种   使用字典初始化方式进行创建的是固定键值类型的字典
//4.1 
var initDictionary:[String:String] = [String :String]()
var initDictionary2:Dictionary<String, String> = Dictionary()

/*Swift和OC中集合对照
在OC中。我们经常使用的数组和字典都是引用类型,而Swift中是值类型,这是由于在Swift中,这些结合类的底层都是struct
枚举值类型。函数,闭包是引用类型
*/
原文地址:https://www.cnblogs.com/mfmdaoyou/p/7055927.html