original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
swift key) set, one is the value ( value) collection. A key collection cannot have duplicate elements, and a collection of values can be duplicated, and the keys and values are paired.
Dictionary Declaration and initialization
the Swift dictionary type is Dictionaryand is also a generic collection.
in declaring a you can use one of the following statements when you type Dictionary.
var studentdictionary1:dictionary<int, String>var StudentDictionary2: [int:string]
The dictionary of the Declaration needs to be initialized to be used, and the dictionary 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: "John Doe", 109: "Harry"]var StudentDictionary2 =[102: "Zhang San", 105: "John Doe", 109: "Harry"]let studentDictionary3 =[102: "Zhang San", 105: "John Doe", 109: "Harry"]
Dictionary traversal
The dictionary traversal process can traverse only the collection of values, or only the collection of keys, or it can traverse at the same time. These traversal processes are implemented through the for-in loop.
The following is a sample code that iterates through a dictionary:
var studentdictionary =[102: "Zhang San", 105: "John Doe", 109: "Harry"]print ("---traversal key---") for StudentID in studentdictionary.keys{Prin T ("study number: \ (StudentID)")}print ("---traversal value---") for studentname instudentdictionary.values {print ("Student: \ (Studentname)")} Print ("---traversal key: Value---") for (Studentid,studentname) in Studentdictionary {print ("\ (StudentID): \ (Studentname)}}
The results of the operation are as follows:
--- Traverse Key ---
School Number: the
School Number: 102
School Number: 109
--- Traversing Values ---
Student: John Doe
Student: Zhang San
Student: Harry
--- traverse key : value ---
: John Doe
102: Zhang San
109: Harry
Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php
Swift 2.0 Study Notes (day 16)--Dictionary collection