Tuples, arrays, dictionaries in Swift

Source: Internet
Author: User

Tuples (tuples) are significantly different from arrays and dictionaries, which make up multiple values into a composite value:

404 Error = (404,"notFound")//HTTP 404 is of type (INT, String), the value is (404, "not Found")

You can name a single element when you define a tuple, and the array of tuples has some similarities:

 $ " OK ")

Arrays and dictionaries (Dictionary) are collections of the same data type, and the dictionary is in the form of a key-value pair, and the array has only values. But the array is actually a special dictionary whose keys correspond to an ordered natural number starting from 0.

The difference between inserting and appending elements of an array:

var arraytest = ["hello"]arraytest.insert ("Swift", Atindex:1)// method of inserting elements arraytest.append ("Swift"  )// methods for appending elements

The following is the basic usage of the dictionary:

Creation of dictionaries:

//two ways to create a dictionaryvar mychildren:dictionary<string,string> = [ " First":"Mao" "Second":"er Mao" "Third":"Sanmao"]var Mydog= [1:"Rhubarb"2:"Erhuang"3:"Xiao Huang"]

Read the dictionary (query):

mychildren["First"]// return to "Da Mao"mydog[3] // back to "little yellow"

The traversal of the dictionary:

// get all the values of the dictionary  for inch mydog.values{  Print ("value:\ (Value)")  }

Tuples, arrays, dictionaries in Swift

Related Article

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.