A dictionary represents a very complex collection that allows the element to be accessed by a key. A dictionary is a set of two parts, one is a set of key (key), andThe other is a collection of values (value). A key collection cannot have duplicate elements, and a collection of values can be duplicated, and the keys and values are paired.
As shown in the dictionary structure of the "school number and students" collection, the number is a key set, can not be repeated, the student is a collection of values, it is possible to repeat.
Hints the collection of keys and values in the dictionary is unordered, even when added in order, and when the keys or values are taken out, they become unordered. The dictionary collection is more suitable for fast access to the value by key, like the English Dictionary, the key is to check the English words, and the value is English words translation and interpretation. Sometimes, an English word will correspond to multiple translations and interpretations, which correspond to the characteristics of the dictionary collection.
First, the dictionary Declaration and Initialization
Swift provides the dictionary struct type for the dictionary, and we can use the following statement when declaring a dictionary.
var studentdictionary:dictionary<int, string>
Where the variable studentdictionary explicitly specifies the type dictionary<int, string>. Where <int, string> is generic, which indicates that the collection of keys is of type Int and that the collection of values is of type String.
The set declared above is actually not available and needs to be initialized, and the collection type is often initialized at the same time as the declaration. The sample code is as follows:
var studentDictionary1: Dictionary <Int, String> = [102: "Zhang San", 105: "Li Si", 109: "Wang Wu"] ①
var studentDictionary2 = [102: "Zhang San", 105: "Li Si", 109: "Wang Wu"] ②
let studentDictionary3 = [102: "Zhang San", 105: "Li Si", 109: "王 五"] ③
var studentDictionary4 = Dictionary <Int, String> () ④
The above code is to declare and initialize the dictionary, code ①~③ line with [102: "Zhang San", 105: "John Doe", 109:
"Harry"] is initialized in a way that is the way the dictionary is represented, as shown in the syntax .
This syntax is similar to the object in JSON, where the dictionary starts with "{" (opening parenthesis) and ends with "}" (the closing parenthesis). Each key is followed by a ":" (colon) and a "," (comma)-separated key-value pair.
The ③ line is a let declaration dictionary, which is the immutable dictionary, which must be initialized at the same time as the declaration and cannot be modified once initialized.
The code ④ line is initialized with an empty dictionary, the key collection is of type int, the value collection is string, and there is no element after initialization.
Ii. Modification of the dictionary
We can modify the elements in the dictionary by appending, deleting, and replacing them. The addition of a dictionary element is simple, as long as a valid value is assigned to a non-existent key, a "key-value" pair element is appended.
There are two ways to delete a dictionary element, one is to assign a key to nil, you can delete the element, and the other is to delete the element by means of the dictionary's Removevalueforkey method, which is the value to be deleted.
There are two methods for dictionary element substitution, one is to assign values directly to an existing key, so that the new value replaces the old value, and the other is replaced by the Updatevalue (Forkey:) method, and the return value of the method is the value to replace.
Let's look at an example:
var studentDictionary = [102: "Zhang San", 105: "Li Si", 109: "Wang Wu"] ①
studentDictionary [110] = "Dong Liu" ②
println ("Class size: \ (studentDictionary.count)") ③
let dismissStudent = studentDictionary.removeValueForKey (102) ④
println ("Fired students: \ (dismissStudent)") ⑤
studentDictionary [105] = nil ⑥
studentDictionary [109] = "Zhang San" ⑦
let replaceStudent = studentDictionary.updateValue ("Li Si", forKey: 110) ⑧
println ("Replaced students are: \ (replaceStudent)") ⑨
The output results are as follows:
Class Size:4
Expelled students: Zhang San
The students to be replaced are: Dong Liu
The above Code section ① is declaring and initializing the dictionary studentdictionary, the ② line code append key is "A", a value of "Dong Liu" an element, the ③ line code is the number of students to print class,count is the property of the dictionary, Returns the length of the dictionary.
Line ④ and line ⑥ are all delete elements, the ④ line code is to use the Removevalueforkey method to delete the element, Dismissstudent is the return value, it keeps the deleted element. So we print the output in line ⑤ dismissstudent is "expelled students: Zhang San". Line ⑥ studentdictionary[105] = Nil statement is a direct assignment nil can also delete the corresponding element.
Line ⑦ and line ⑧ replace the old element, and if the key of line ⑦ does not exist, the result is a new "key-value" pair element appended to the dictionary. Line ⑧ is replaced by the Updatevalue (Forkey:) method, the return value of the method is "Dong Liu", and the ⑨ Line code is printed "The student being replaced is: Dong Liu".
Third, dictionary traversal
The dictionary traversal collection is also an important operation of the dictionary. Unlike arrays, the dictionary has two collections, so the traversal can traverse only the collection of values, or only the collection of keys, or at the same time. These traversal procedures are implemented through a for-in loop.
The following is a sample code that iterates through a dictionary:
var studentDictionary = [102: "Zhang San", 105: "Li Si", 109: "King Wu"]
println ("--- traversal key ---")
for studentID in studentDictionary.keys {①
println ("Student ID: \ (studentID)")
}
println ("--- traversal value ---")
for studentName in studentDictionary.values {②
println ("Student: \ (studentName)")
}
println ("--- traversal key: value ---")
for (studentID, studentName) in studentDictionary {③
println ("\ (studentID): \ (studentName)")
}
The results of the operation are as follows:
--- traversal key ---
Study No.:
School Number:102
School Number:109
--- traversal value ---
Student: John Doe
Student: Zhang San
Student: Harry
--- traversal key : value ---
£ º John Doe
102: Zhang San
109: Harry
As we can see from the code above, we have 3 ways to traverse the dictionary, all of which use the for in statement. The ① code iterates through the key collection, where keys is a dictionary property and can return a collection of all the keys. The ② line code iterates through a collection of values, where values is a dictionary property and can return a collection of all values. The ③ line code iterates through the dictionary elements that are fetched, (StudentID, Studentname) is a tuple type, which consists of a key variable StudentID and a value variable studentname.
For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485
Welcome to Luxgen iOS Classroom public Platform
Swift Dictionary Collection