这篇来说说字典的定义用法。
定义字典:
let dd = [Int:String]()
print(dd)//定义一个空字典,而且定义了它的key为int,value为string
也可以这么定义个空字典:
let d:[Int:String] = [:]
print(d)
var emptyDict:[String:String] = [:]
print(emptyDict)
定义一个有值的字典:
let dic1:[Int:String] = [1:"one",2:"two"]
print(dic1)
print(dic1[1] as Any)
let dic2 = ["name":"007","age":"xiaoli"]
print(dic2)
print(dic2["name"] as Any)
let dict = ["name":"张","age":18] as [String : Any]
print(dict);
print(dict["age"] as Any)
定义一个字典数组:
let dic3:[[String:AnyObject]] = [
["name":"ooo" as AnyObject,"age":18 as AnyObject],
["name":"oop" as AnyObject,"age":18 as AnyObject]
]
print(dic3)
总结
字典的定义就样子,有些我还是不明白,后面随着理解加深及时补充。