Swift字典用于存储相同类型的无序值列表。Swift严格检查,即使错误也不允许您在字典中输入错误的类型。
Swift字典使用唯一的标识符作为密钥存储一个值,后来可以通过相同的键引用和查找值。与数组中的项目不同,字典中的项目没有指定的顺序。您可以使用字典,当你需要根据自己的标识符中查找值。
字典键可以是整数或字符string而不受限制,但在字典中它应该是唯一的。
如果您将创建的字典分配给变量,则它始终是可变的,这意味着您可以通过添加,删除或更改其项目来更改它。但是,如果您将字典分配给常量,则该字典是不可变的,并且其大小和内容不能更改。
创建字典
您可以使用以下初始化程序语法创建某个类型的空字典 -
var someDict = [KeyType: ValueType]()
您可以使用以下简单语法创建一个空字典,其键将为Int类型,相关值将为字符string -
var someDict = [Int: String]()
以下是从一组给定值创建字典的示例 -
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
访问字典
You can retrieve a value from a dictionary by using subscript syntax, passing the key of the value you want to retrieve within square brackets immediately after the name of the dictionary as follows −
var someVar = someDict[key]
Let"s check the following example to create, initialize, and access values from a dictionary −
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]varsomeVar=someDict[1]println("Value of key = 1 is (someVar)")println("Value of key = 2 is (someDict[2])")println("Value of key = 3 is (someDict[3])")
When the above code is compiled and executed, it produces the following result −
Value of key = 1 is Optional("One")
Value of key = 2 is Optional("Two")
Value of key = 3 is Optional("Three")
Modifying Dictionaries
You can use updateValue(forKey:) method to add an existing value to a given key of the dictionary. This method returns an optional value of the dictionary"s value type. Here is a simple example −
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]varoldVal=someDict.updateValue("New value of one",forKey:1)varsomeVar=someDict[1]println("Old value of key = 1 is (oldVal)")println("Value of key = 1 is (someVar)")println("Value of key = 2 is (someDict[2])")println("Value of key = 3 is (someDict[3])")
When the above code is compiled and executed, it produces the following result −
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")
You can modify an existing element of a dictionary by assigning new value at a given key as shown in the following example −
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]varoldVal=someDict[1]someDict[1]="New value of one"varsomeVar=someDict[1]println("Old value of key = 1 is (oldVal)")println("Value of key = 1 is (someVar)")println("Value of key = 2 is (someDict[2])")println("Value of key = 3 is (someDict[3])")
When the above code is compiled and executed, it produces the following result −
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")
Remove Key-Value Pairs
您可以使用removeValueForKey()方法从字典中删除键值对。如果存在此方法,则该方法将删除键值对,并返回已删除的值,如果不存在值,则返回nil。这是一个简单的例子 -
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]varremovedValue=someDict.removeValueForKey(2)println("Value of key = 1 is (someDict[1])")println("Value of key = 2 is (someDict[2])")println("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值。这是一个简单的例子 -
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]someDict[2]=nilprintln("Value of key = 1 is (someDict[1])")println("Value of key = 2 is (someDict[2])")println("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循环遍历字典中的整个键值对集合,如以下示例所示:
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]for(key,value)insomeDict{println("Dictionary key (key) - Dictionary value (value)")}
当上述代码被编译和执行时,它产生以下结果 -
Dictionary key 2 - Dictionary value Two
Dictionary key 3 - Dictionary value Three
Dictionary key 1 - Dictionary value One
您可以使用枚举()函数,它返回项目的索引及其(键值)对,如下例所示:
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]for(key,value)inenumerate(someDict){println("Dictionary key (key) - Dictionary value (value)")}
当上述代码被编译和执行时,它产生以下结果 -
Dictionary key 0 - Dictionary value (2, Two)
Dictionary key 1 - Dictionary value (3, Three)
Dictionary key 2 - Dictionary value (1, One)
转换为数组
您可以从给定的字典中提取键值对列表,为键和值构建单独的数组。这是一个例子 -
importCocoavarsomeDict:[Int:String]=[1:"One",2:"Two",3:"Three"]letdictKeys=[Int](someDict.keys)letdictValues=[String](someDict.values)println("Print Dictionary Keys")for(key)indictKeys{println("(key)")}println("Print Dictionary Values")for(value)indictValues{println("(value)")}
当上述代码被编译和执行时,它产生以下结果 -
Print Dictionary Keys
2
3
1
Print Dictionary Values
Two
Three
One
计数财产
您可以使用字典的只读计数属性来查找字典中的项目数,如下所示:
importCocoavarsomeDict1:[Int:String]=[1:"One",2:"Two",3:"Three"]varsomeDict2:[Int:String]=[4:"Four",5:"Five"]println("Total items in someDict1 = (someDict1.count)")println("Total items in someDict2 = (someDict2.count)")
当上述代码被编译和执行时,它产生以下结果 -
Total items in someDict1 = 3
Total items in someDict2 = 2
空的财产
您可以使用字典的只读空属性来查找字典是否为空,如下所示:
importCocoavarsomeDict1:[Int:String]=[1:"One",2:"Two",3:"Three"]varsomeDict2:[Int:String]=[4:"Four",5:"Five"]varsomeDict3:[Int:String]=[Int:String]()println("someDict1 = (someDict1.isEmpty)")println("someDict2 = (someDict2.isEmpty)")println("someDict3 = (someDict3.isEmpty)")
当上述代码被编译和执行时,它产生以下结果 -
someDict1 = false
someDict2 = false
someDict3 = true