Detailed examples of swift tuples developed by IOS

Time:2022-5-14

Detailed examples of swift tuples developed by IOS

A tuple is a composite value composed of multiple values. Values in tuples can be of any type, and the type of each element can be different.

Tuple declaration

//General statement
var point = (5,2)

var httpResponse = (404, "Not Found")

//Define type declaration
var point2 : (Int,Int,Int) = (10,5,2)

var httpResponse2 : (Int,String) = (200,"ok")

Tuple unpacking

var point = (5,2)

var httpResponse = (404, "Not Found")

let (x,y) = point
// x = 5 , y = 2

var (statuseCode, statuseMessage) = httpResponse
// statuseCode = 404 , statuseMessage = "Not Found"

//At this time, because the tuple (x, y) is of let type, the values of X and y cannot be changed; And (statusecode, statusmessage) is of VaR type, so the values of statusecode and statusmessage can be changed

X = 10 // report error

Statusecode = 405 // no error

Tuples can also be unpacked according to the index like arrays, so start from 0

var point = (5,2)

point. 0 // value is 5
point. 1 // the value is 2

Tuples can also be used as dictionaries to assign a key to each value as a name and unpack according to the name

let point3 = (x:3,y:2)

point3. 0 // value is 3

point3. X // value is 3


let point4 : (x:Int,y:Int) = (5,10)


point4. X // value is 5

point4. Y // value is 10

Partial unpacking, unnecessary values are used_ Indicates that the tuple is partially unpacked

//Partial unpacking

let loginResult = (true, "LXY")

let (isLoginSuccess,_) = loginResult

if isLoginSuccess
{

}

The above is the explanation of IOS swift tuple. If you have any questions, please leave a message and exchange. Thank you for reading. I hope it can help you. Thank you for your support to this site!

Recommended Today

C# LINQ query details

catalogue Basic query Deferred query properties Type filtering Compound from clause Multilevel sorting grouping Joint query join Merge zip () Partition (paging) Parallel LINQ Cancel long running parallel LINQ queries The Ling query provided by c# greatly traverses the query process of the set, and is simple and convenient to use, which is very useful. […]