系统: Mac OS 10.15.2, XCode 11.3,swift 5.0
写作时间:2020-01-08
说明
Dictionary为字典键值对,是比较常用的容器,初始化的格式如下。
var someDict = [KeyType: ValueType]()
Creating Dictionary
创建可以一个空的容器,或者初始化为指定的值
var dict = [Int: String]()
var dict1:[Int: String] = [1:"One", 2:"Two", 3:"Three"]
Sequence Based Initialization
通过key数组, Value数组,创建字典。
var cities = ["Delhi", "Bangalre", "Hyderabad"]
var distance = [2000, 10, 620]
let cityDistanceDict = Dictionary(uniqueKeysWithValues: zip(cities, distance))
Filtering
通过过滤掉不符合条件的子项。
var closeCities = cityDistanceDict.filter{ $0.value < 1000 }
print("\(closeCities)")
// closeCities > ["Bangalore": 10, "Hyderabad": 620]
Dictionary Grouping
给字典分组,下面为对第一个字符相同的子项划分为同一组。
var city = ["Delhi", "Bangalore", "Hyderabad", "Dehradun", "Bihar"]
var GroupedCities = Dictionary(grouping: city) { $0.first! }
print("\(GroupedCities)")
// ["D" :["Delhi","Dehradun"], "B" : ["Bengaluru","Bihar"], "H" : ["Hyderabad"]]
Accessing Dictionaries
方位字典的value通过key值索引获取
var someVar = someDict[key]
var someDict:[Int: String] = [1:"One", 2:"Two", 3:"Three"]
var someVar = someDict[1]
print("Value of key = 1 is \(someVar)")
print("Value of key = 2 is \(someDict[2])")
print("Value of key = 3 is \(someDict[3])")
//Value of key = 1 is Optional("One")
//Value of key = 2 is Optional("Two")
//Value of key = 3 is Optional("Three")
Modifying Dictionaries
修改值可以通过方法updateValue
, 也可以通过赋值的方式。
var oldVal = someDict.updateValue("New value of one", forKey: 1)
someVar = someDict[1]
someDict[2] = "New value of two"
print("Old value of key = 1 is \(oldVal)")
print( "Value of key = 1 is \(someVar)" )
print( "Value of key = 2 is \(someDict[2])" )
print( "Value of key = 3 is \(someDict[3])" )
//Old value of key = 1 is Optional("One")
//Value of key = 1 is Optional("New value of one")
//Value of key = 2 is Optional("New value of two")
//Value of key = 3 is Optional("Three")
Remove Key-Value Pairs
删除键值对,可以通过方法removeValue
,或者通过设置值为nil。
someDict = [1:"One", 2:"Two", 3:"Three"]
var removeValue = someDict.removeValue(forKey: 2)
someDict[3] = nil
print( "Value of key = 1 is \(someDict[1])" )
print( "Value of key = 2 is \(someDict[2])" )
print( "Value of key = 3 is \(someDict[3])" )
//Value of key = 1 is Optional("One")
//Value of key = 2 is nil
//Value of key = 3 is nil
Iterating Over a Dictionary
遍历字典正确的方式如下:
someDict = [1:"One", 2:"Two", 3:"Three"]
for (key, value) in someDict {
print("Dictionary key \(key) - Dictionary value \(value)")
}
//Dictionary key 3 - Dictionary value Three
//Dictionary key 1 - Dictionary value One
//Dictionary key 2 - Dictionary value Two
注意:如果遍历someDict.enumerated()
,实际上得到的key为从0开始的索引,value为keyValue子项(key: 3, value: "Three")
。
for (index, keyValue) in someDict.enumerated() {
print("index \(index) - Dictionary value \(keyValue)")
}
//index 0 - Dictionary value (key: 3, value: "Three")
//index 1 - Dictionary value (key: 1, value: "One")
//index 2 - Dictionary value (key: 2, value: "Two")
Convert to Arrays
字典的keys和values都是数组,可以单独转换为数组用. 注意转换是为数组,不是Set不重复数组,可以通过观察两个value都为Two,实际上两个Two都打印了出来。
someDict = [1:"One", 2:"Two", 3:"Two"]
let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)
print("Print Dictionary Keys")
for (key) in dictKeys {
print("\(key)")
}
print("Print Dictionary Values")
for (value) in dictValues {
print("\(value)")
}
//Print Dictionary Keys
//2
//3
//1
//Print Dictionary Values
//Two
//Two
//One
The count Property
字典的数量,通过count属性来获取。
var someDict1: [Int: String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2: [Int: String] = [4:"Four", 5:"Five"]
print("Total items in someDict1 = \(someDict1.count)")
print("Total items in someDict2 = \(someDict2.count)")
//Total items in someDict1 = 3
//Total items in someDict2 = 2
The empty Property
判断Dictionary是否可控,可以通过属性isEmpty来判断
var someDict3:[Int:String] = [Int:String]()
print("someDict1 = \(someDict1.isEmpty)")
print("someDict2 = \(someDict2.isEmpty)")
print("someDict3 = \(someDict3.isEmpty)")
//someDict1 = false
//someDict2 = false
//someDict3 = true
参考
https://www.tutorialspoint.com/swift/swift_dictionaries.htm