Swift's tuple type

Source: Internet
Author: User

One, the tuple type is N any type of data composition (n>=0), the data that make up the tuple type can be called "element"

Two, the definition of the tuple

such as: let position = (X:10.5,Y:20)//Two elements of a tuple

Let person = (name: "Jack")//Element tuple

Let data = ()//0 elements of a tuple

Another example: let position = (10.5,20)//Omitting the element name of a tuple

Let person = ("Jack")//a tuple that omits element names

Another example: Var (x, y) = (10.5,20)

var (name) = ("Jack")

The above two methods do not declare the types of elements of a tuple, the system automatically determines its data type, it can also specify its own data type for each element of the tuple, and if the element type is specified, it is not possible to specify its name for the element, which is a syntax error.

For example: Var person: (int,string) = ("Jack")//correct

var person: (int,string) = (age:23,name: "Jack")//Error

Third, the tuple's access way

It can be viewed as an object or as an array

As the position above

1.position.x

2.position.0

Both of these methods can be accessed to 10.5

In addition, when a tuple is defined as a constant, all elements of the tuple are constants

For example: var person = ("Jack")

var (_,name) = person//Remove only the value of name

Swift's tuple type

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.