Swift language guide (8)-tuples of language basics

Source: Internet
Author: User

 

Tuples

Tuples (Tuples) Combines multiple values into a composite value. The value in the element group can be of any type, and each element does not need to be of the same type (the types of each element are independent and do not interfere with each other-Joe. Huang ).

In the following example,(404, "Not Found ")Is a descriptionHTTPStatus code.HTTPThe status code is when youWEBWhen the server requests a page, the server returns a special value.WEBServer) requests a webpage that does not exist, and the returned status code is404 Not Found:

1 let http404Error = (404, "Not Found") 2 // http404Error is of type (Int, String), and equals (404, "Not Found ") 3 // The Error Type of http404 is (integer, string type), and its value is (404, "Not Found ")

(404, "Not Found ")TuplesIntValue andStringValue combination, indicatingHTTPTwo values of the status code: a number and a textual description that people can read. It can be understood as follows: "A type is(Int, String)".

You can create a metagroup by arranging the types in any way, or you can include multiple different types of tuples as you like. The creation type is (Int, Int, Int) Or (String, Bool.

The content of the tuples can be decomposed (Decompose) Restore and assign values to individual constants or variables, and then access them as usual:

1 let (statusCode, statusMessage) = http404Error2 println ("The status code is \ (statusCode )") 3 // prints "The status code is 404" // output "status code 404" 4 println ("The status message is \ (statusMessage )") 5 // prints "The status message is Not Found // output" status information Not Found"

If you only need a certain part of the value of a tuples, you can use the underscore (_) to mark them when breaking them down to ignore other parts:

1 let (justTheStatusCode, _) = http404Error2 println ("The status code is \ (justTheStatusCode )") 3 // prints "The status code is 404 // The output" status code 404"

Alternatively, you can access the values of each element of a tuples starting with 0 through the tuples index:

1 println ("The status code is \ (http404Error. 0) ") 2 // prints" The status code is 404 "// output" status code 404 "3 println (" The status message is \ (http404Error. 1) ") 4 // prints" The status message is Not Found // The output "status code is Not Found"
5 // This method is similar to accessing the values of each element of the array through the subscript index -- Joe. Huang

You can also name the elements of the tuples separately when defining them:

1 let http200Status = (statusCode: 200, description: "OK")

After naming each element of the tuples, you can use the element name to access the values of each element:

1 println ("The status code is \ (http200Status. statusCode) ") 2 // prints" The status code is 200 "// output" status code 404 "3 println (" The status message is \ (http200Status. description) ") 4 // prints" The status message is OK // output "status information OK"

Tuples are useful when used as function return values. A function to obtain webpage content may return(Int, String)To describe whether the webpage loading is successful or failed. The function returns two completely different types of values to describe the returned state or result. The information provided by (this return method) is much more useful than returning a single value of a fixed type. For more information, see functions with multiple return values (which will be translated later ).

Note:

Tuples are useful for temporary combinations of multiple correlated values. However, they are not suitable for creating complex data structures. If the lifecycle of your data structures exceeds the scope of temporary use (not temporary value, it should be cached through classes or modeling -- Joe. huang), please use it as a class or structure modeling, rather than using tuples for storage. For more information, see classes and structures (which will be translated later ).

 

Thank you, Swifter-QQ group: 362232993 ~

Fork: https://github.com/Joejo/Swift-lesson-for-chinese

 

 

 

 

 

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.