Swift Learning tuple (tuple)

Source: Internet
Author: User

Defined

Tuples are composed of several types of data, the data that makes up a tuple is called an element, and the type of each element can be arbitrary.

Usage One

Let Tuples1 = ("Hello", "World", 2017)

Tuples are the same as arrays, and the corners of their elements can be tuple1.0 tuple1.1 tuple1.2 starting from 0.

Print ("Get first value \ (tuples1.0), get the second value \ (tuples1.1), get the third value \ (tuples1.2)")

Usage Two

Let Tuples2 = (first: "Hello", Second: "World", third:2017)

The values of the tuples used in this use can be Tuple2.first tuple2.second Tuple3.third

Print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.third)" )

Usage Three

Variable receiving numerical method

Let (x, Y, Z) = ("Hello", "World", 2017);

Print ("Get X is \ (x) get y are \ (y) get Z is \ (z)")

Use _ to remove the value of some elements ignore _ corresponding value

Let (A, _, B) = ("Hello", "World", 2017);

Print ("Get X is \ (a) Get z-is \ (b)")

Tuples Worth modifying

Let Tuples2 = (first: "Hello", Second: "World", third:2017)

The values of the tuples used in this use can be Tuple2.first tuple2.second Tuple3.third

Print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.third)" )

This will cause an error

Tuples2.first = "Test"; The let before tuple2 should be changed to Var to make tuples into variable tuples

var tuples2 = (First: "Hello", Second: "World", third:2017)

The values of the tuples used in this use can be Tuple2.first tuple2.second Tuple3.third

var print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.thi RD) ")

Tuples2.first = "Test";

Print ("Get the first value \ (Tuples2.first), get the second value \ (Tuples2.second), get the third value \ (Tuples2.third)" )

Advanced Applications for tuples

Defines a function that can exchange two elements in an array

Func swap<t> (_ Nums:inout [T], _ P:int, _ Q:int) {

(Nums[p], nums[q]) = (Nums[q], nums[p])

}

var array = ["Hello", "World") as [any]

For value in array {

print (value);

}

Swap (&array, 0, 2)

For value in array {

print (value);

}

Swift Learning tuple (tuple)

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.