Swift 字典

Swift 字典

Swift 4 字典 用于存储相同类型的无序值列表。Swift 4 进行严格检查,即使错误输入,也不允许您在字典中输入错误类型。

Swift 4 字典使用称为 的唯一标识符存储值,稍后可以通过相同的键引用和查找值。与数组中的项目不同,字典中的项目没有指定的顺序。当需要根据其标识符查找值时,可以使用 字典

字典键可以是整数或字符串,没有限制,但在字典内必须唯一。

如果将创建的字典分配给变量,则它始终是可变的,这意味着您可以通过添加、删除或更改其项来更改它。但是,如果将字典分配给常数,则该字典是不可变的,其大小和内容都不能更改。

创建字典

可以使用以下初始化器语法创建特定类型的空字典−

var someDict = [KeyType: ValueType]()

您可以使用以下简单的语法来创建一个空字典,其键将是Int类型,相应的值将是字符串 –

var someDict = [Int: String]()

下面是一个以给定值创建字典的示例 –

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

基于序列的初始化

Swift 4 允许您从数组(键值对)创建字典。

var cities = [“Delhi”,”Bangalore”,”Hyderabad”]

你可以使用以下简单的语法来创建一个空字典,其键的类型为Int,关联的值为字符串 –

var Distance = [2000,10, 620]

以下是从一组给定值创建字典的示例

let cityDistanceDict = Dictionary(uniqueKeysWithValues: zip(cities, Distance))

上述的代码将创建一个以城市为键,距离为值的字典。

过滤

Swift 4允许你从字典中过滤值。

var closeCities = cityDistanceDict.filter { $0.value < 1000 }

如果我们运行上述代码,我们的closeCities字典将是。

["Bangalore" : 10 , "Hyderabad" : 620]

字典分组

Swift 4允许您对字典值进行分组。

var cities = ["Delhi","Bangalore","Hyderabad","Dehradun","Bihar"]

您可以使用以下简单语法根据字母顺序对字典的值进行分组。

var GroupedCities = Dictionary(grouping: cities ) { $0.first! }

以上代码的结果将会是:

["D" :["Delhi","Dehradun"], "B" : ["Bengaluru","Bihar"], "H" : ["Hyderabad"]]

访问字典

你可以使用子脚本语法,将要检索的值的键置于方括号中,紧接在字典名称后面,从而从字典中检索一个值,如下所示:

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")

修改字典

你可以使用 updateValue(forKey:) 方法向字典的特定键中添加现有值。该方法返回字典值类型的可选值。以下是一个简单的示例:

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var oldVal = someDict.updateValue("New value of one", forKey: 1)
var someVar = someDict[1]

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("Two")
Value of key = 3 is Optional("Three")

您可以通过在给定键上赋予新值来修改字典的现有元素,如下面的示例所示−

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var oldVal = someDict[1]
someDict[1] = "New value of one"
var someVar = someDict[1]

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("Two")
Value of key = 3 is Optional("Three")

删除键值对

你可以使用 removeValueForKey() 方法从字典中删除一个键值对。该方法会删除存在的键值对并返回被删除的值,如果值不存在则返回nil。以下是一个简单的示例 –

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var removedValue = someDict.removeValue(forKey: 2)

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 Optional("Three")

您还可以使用下标语法通过为特定键赋值 nil 来从字典中移除键值对。以下是一个简单的示例:

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

someDict[2] = 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 Optional("Three")

遍历字典

您可以使用 for-in 循环遍历字典中的所有键值对,如下面的示例所示:

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

for (index, keyValue) in someDict.enumerated() {
   print("Dictionary key \(index) - Dictionary value \(keyValue)")
}

当上述代码被编译并执行时,会产生以下结果−

Dictionary key 2 - Dictionary value Two
Dictionary key 3 - Dictionary value Three
Dictionary key 1 - Dictionary value One

你可以使用 enumerate() 函数,该函数将索引与项的(键,值)对一起返回,如下面的示例所示:

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
for (key, value) in someDict.enumerated() {
   print("Dictionary key \(key) - Dictionary value \(value)")
}

当上述代码被编译和执行时,产生以下结果 −

Dictionary key 0 - Dictionary value (key: 2, value: "Two")
Dictionary key 1 - Dictionary value (key: 3, value: "Three")
Dictionary key 2 - Dictionary value (key: 1, value: "One")

转换为数组

您可以从给定的字典中提取键值对列表,构建对应的键数组和值数组。这是一个示例−

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

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
Three
One

count属性

您可以使用字典的只读 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

empty 属性

你可以使用字典的只读属性 empty 来判断一个字典是否为空,如下所示 −

var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2:[Int:String] = [4:"Four", 5:"Five"]
var someDict3:[Int:String] = [Int:String]()

print("someDict1 = \(someDict1.isEmpty)")
print("someDict2 = \(someDict2.isEmpty)")
print("someDict3 = \(someDict3.isEmpty)")

当上述代码被编译和执行时,会产生以下结果−

someDict1 = false
someDict2 = false
someDict3 = true

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程