Swift中Dictionary

来源:互联网 发布:闲来麻将服务器源码 编辑:程序博客网 时间:2024/06/05 08:02

//定义字典

let dict1 = ["name" :"小小","age" : "6","height" : 1.88] as [String :Any]

var dict2 = [:] as [String:Any]

var dict3 = Dictionary<String,Any>()


//字典的基本操作(增删改查)

var dictM = Dictionary<String,Any>()

dictM["name"] ="小小"

dictM["age"] = 18

dictM["height"] = 1.88

dictM["phone"] ="+86 130xxxxxxxx"

//当字典里有插入的key那么下面操作就会把原来的value值改掉

dictM["name"] ="小黑"

print(dictM)

dictM.removeValue(forKey:"name")

//dictM.removeAll()


//便利字典

for key indictM.keys {

    print(key)

}


for value indictM.values {

    print(value)

}


for (key,value) indictM {

    print(key)

    print(value)

}


//合并字典

for (key,value) indictM {

    dict2[key] = value

}

0 0
原创粉丝点击