Introduction to Swift (III.)--tuple (tuple)

Source: Internet
Author: User

Defined

A tuple is an object that contains a number of associated variables.

Creation of tuples
var newTuple = ("kt",20)//由于Swift的类型推导,newTuple被推导为(String,Int)类型的变量
Tuple's Unbind

Once a few variables are joined together and a tuple is formed, they are "bound", and the tuple needs to be unpacked in order to remove a single piece of data from a tuple.

Direct unbind

Direct Unbind is the simplest way to unbind, as long as you define several variables corresponding to the variable one by one in a tuple.

var newTuple = ("kt",20)var (name , age) = newTupleprintln("name = \(name)")println("age = \(age)")

Output Result:

name = ktage = 20
Filter element Unbind

Direct unbind is simple to use, but may add some amount of code. If there are 100 data in the tuple, and we are only interested in one data, we can use _ instead of the variable names we are not interested in, that is, filter them out. The code is as follows:

var newTuple = ("kt",20)var (name , _) = newTupleprintln("name = \(name)")

Output Result:

name = kt
Subscript Unbind

If you still feel the need to write the name of a variable, you can also use the simpler table below to unbind it. In this case, the tuple can be used as an array, directly write out the variables in the tuple subscript can be. (Subscript starting from 0)

var newTuple = ("kt",20)println("name = \(newTuple.0)"//输出结果和之前一样。

However, it is important to note that such a formulation has a disadvantage: subscript can not be represented by a variable, that is, the wording is wrong:

var newTuple = ("kt",200println("name = \(newTuple.index)")//错误,报错:“(String, Int)doesn‘tnot‘index‘”

Attempting to cross-border access to a nonexistent data can result in a compilation error.

Variable name unbind

If you specify the variable name of a variable when you define a tuple, you can also unbind it based on the variable name of the variable.

var"kt", age: 20)println("name = \(newTuple.name)")

As with subscript unbind, the variable name here cannot be replaced with a string variable with a value of "name".

Summarize

The concept of tuple (tuple) is a relatively new concept for program apes that have no access to scripting languages. But tuples are neither complex nor mysterious, and many times they can be solved with struct structures or classes. A tuple can be understood as a lightweight data structure that can hold only the information but not define the method.

Appendix Swift Introductory Series Tutorial Swift Introduction (i)--Basic Syntax Swift Introduction (ii)--character and string

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to Swift (III.)--tuple (tuple)

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.