Swift 2.0 Study Notes (day 16)--Dictionary collection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.