Latest web development tutorials

Swift dictionary

A collection of the same type used to store data dictionary Swift disorder, Swift dictionary enforces type detection element, if a different type of error occurs.

Swift dictionary for each value (value) is associated unique key (key), the key identifier as the dictionary data value.

And an array of different data items, data dictionary item and there is no specific order. We need to use the dictionary identifier (key) to access the data, and we use this approach to a large extent the dictionary meaning of the same methods in the real world.

Swift dictionary key no restrictions on the type can be integer or string, but must be unique.

If you create a dictionary, and assigned to a variable, the dictionary is created that can be modified. This means that after you create the dictionary, you can add, delete, modify, change the way the dictionary project. If a dictionary is assigned to a constant, you can not modify the dictionary, and the dictionary size and content can not be modified.


Create dictionary

We can use the following syntax to create a specific type of empty dictionary:

var someDict =  [KeyType: ValueType]()

The following is creating an empty dictionary, type the key for Int, String type value is a simple syntax:

var someDict = [Int: String]()

The following is to create a dictionary instance:

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

Access dictionary

We can access elements of the array according to the index dictionary, the syntax is as follows:

var someVar = someDict[key]

By the following examples, we can learn how to create, initialize, access dictionaries:

import Cocoa

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

var someVar = someDict[1]

print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

The above program execution output is:

key = 1 的值为 Optional("One")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

Modifying a dictionary

We can useupdateValue (forKey :) add or update the contents of the dictionary.If the key does not exist, add the value, if there is to modify the key corresponding value. updateValue (_: forKey :) Optional method returns a value. Examples are as follows:

import Cocoa

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

var oldVal = someDict.updateValue("One 新的值", forKey: 1)

var someVar = someDict[1]

print( "key = 1 旧的值 \(oldVal)" )
print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

The above program execution output is:

key = 1 旧的值 Optional("One")
key = 1 的值为 Optional("One 新的值")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

You can also modify the value of a dictionary by specifying the key, as follows:

import Cocoa

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

var oldVal = someDict[1]
someDict[1] = "One 新的值"
var someVar = someDict[1]

print( "key = 1 旧的值 \(oldVal)" )
print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

The above program execution output is:

key = 1 旧的值 Optional("One")
key = 1 的值为 Optional("One 新的值")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

Remove Key-Value pair

We can useremoveValueForKey () method to remove the dictionary key-value pairs.If the key exists, the method returns the removed value, if there is no return nil. Examples are as follows:

import Cocoa

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

var removedValue = someDict.removeValueForKey(2)

print( "key = 1 的值为 \(someDict[1])" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

The above program execution output is:

key = 1 的值为 Optional("One")
key = 2 的值为 nil
key = 3 的值为 Optional("Three")

You can also to remove key-value by specifying the key value of nil (key - value) pairs. Examples are as follows:

import Cocoa

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

someDict[2] = nil

print( "key = 1 的值为 \(someDict[1])" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

The above program execution output is:

key = 1 的值为 Optional("One")
key = 2 的值为 nil
key = 3 的值为 Optional("Three")

Traversal dictionary

We can use thefor-in loop to iterate over a dictionary of key-value pairs.Examples are as follows:

import Cocoa

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

for (key, value) in someDict {
   print("字典 key \(key) -  字典 value \(value)")
}

The above program execution output is:

字典 key 2 -  字典 value Two
字典 key 3 -  字典 value Three
字典 key 1 -  字典 value One

We can also use enumerate () method to the dictionary traversal, returns the index of the dictionary and the (key, value) pairs, examples are as follows:

import Cocoa

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

for (key, value) in someDict.enumerate() {
    print("字典 key \(key) -  字典 (key, value) 对 \(value)")
}

The above program execution output is:

字典 key 0 -  字典 (key, value) 对 (2, "Two")
字典 key 1 -  字典 (key, value) 对 (3, "Three")
字典 key 2 -  字典 (key, value) 对 (1, "One")

Dictionary into an array

You can extract the keys of the dictionary (key-value) pairs, and converted to a separate array. Examples are as follows:

import Cocoa

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

let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)

print("输出字典的键(key)")

for (key) in dictKeys {
    print("\(key)")
}

print("输出字典的值(value)")

for (value) in dictValues {
    print("\(value)")
}

The above program execution output is:

输出字典的键(key)
2
3
1
输出字典的值(value)
Two
Three
One

count property

We can use the read-only attributecount to calculate the number of key-value pair dictionary:

import Cocoa

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

print("someDict1 含有 \(someDict1.count) 个键值对")
print("someDict2 含有 \(someDict2.count) 个键值对")

The above program execution output is:

someDict1 含有 3 个键值对
someDict2 含有 2 个键值对

isEmpty property

Y we canisEmpty read-only attribute to determine whether the dictionary is empty, returns a Boolean value:

import Cocoa

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

The above program execution output is:

someDict1 = false
someDict2 = false
someDict3 = true