#字段 dictionary
1 2 3
| var animalGroupsDict = ["whales":"pod", "geese":"flock", "lions":"pride"]
var groupsDict = [String:String]()
|
##常见用法
1 2 3 4
| var lifeSpanDict = ["African Grey Parrot": 50...70, "Tiger Salamander": 12...15, "Bottlenose Dolphin": 20...30]
var averageLifeSpanDict = [String:Range<Int>]()
|
… 运算符代表某个区间 ,如 50… 70就是50到70之间的某个数
1 2 3 4 5
|
animalGrounpsDict["crows"] = "murder" animalGrounpsDict["monkeys"] = "troop"
|
1 2
| //the count method is available to all collections, count方法所有的集合都有 animalGrounpsDict.count
|
1 2
| //Removing items from a dictionary animalGroupsDict["monkeys"] = nil
|
1 2
| //Update a value animalGrounpsDict["monkeys"] = ["barrel"]
|
1 2 3
| var group = animalGrounpsDict.updateValue["gaggle", forkey:"geese"] group.dynamicType (结果为 Optional<String>.Type) //首先更新gaggle的种类为geese,并返回 geese ,通过group.dynamicType获取group的类型
|
1 2 3 4 5 6 7 8
| //Retrieving the value for a particular key,获取key对应的值 let groupOfWhales = animalGroupsDict["whales"]
if letgrounpOfWhales = animalGroupsDict[“whales”]{ // 如果whales对应的value存在 }else{ //如果whales对应的value不存在 }
|
感谢鼓励