Swift Dictionary Collection-standby

Source: Internet
Author: User

The Swift dictionary represents a very complex collection that allows elements to be accessed by a key. A dictionary is a set of two parts, one is a set of key (key), and the 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.

Dictionary Declaration and initialization

The Swift dictionary type is Dictionary and is also a generic collection.

you can use one of the following statements when declaring a dictionary type.

[HTML]View PlainCopyprint?
    1. var studentdictionary1:dictionary<Int, String>
    2. 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:

[HTML]View PlainCopyprint?
    1. var studentdictionary1:dictionary<Int, String>
    2. ê= [102: "Zhang San", 105: "John Doe", 109: "Harry"]
    3. var studentDictionary2 = [102: "Zhang San", 105: "John Doe", 109: "Harry"]
    4. 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:

[HTML]View PlainCopyprint?
  1. var studentdictionary = [102: "Zhang San", 105: "John Doe", 109: "Harry"]
  2. Print ("---traversal key---")
  3. For StudentID in Studentdictionary.keys {
  4. Print ("School Number: \ (StudentID)")
  5. }
  6. Print ("---traversal value---")
  7. For Studentname in Studentdictionary.values {
  8. Print ("Student: \ (studentname)")
  9. }
  10. Print ("---traversal key: Value---")
  11. For (StudentID, studentname) in Studentdictionary {
  12. Print ("\ (StudentID): \ (studentname)")
  13. }

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

105: John Doe

102: Zhang San

109: Harry

Swift Dictionary Collection-standby

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.