Swift's tuple

Source: Internet
Author: User

A tuple is a compound value type that composes multiple values into a single composition. Each value inside a tuple is of any type, and the type of each value can be different from each other.

For example, (404, "not Found") is a tuple used to describe the HTTP state. An HTTP status code is a specific value returned by the Web service when you request a Web page. 404 Not Found is a status code that is returned when you request a page and the page does not exist.

Let Http404error = (404, "not Found")

Http404error is of type (int,string), with a value of (404, "not Found")

Tuples (404, "not Found") integrate an int's word count and a string of two values for a readable string to illustrate the HTTP status encoding. It can be said that "a tuple of type (int,string)".

You can create a tuple from any type combination, and the tuple can contain the different types you want. Tuples (int,int,int) or tuples (String,bool) do not have any problem errors, or combine any type you need.

When you visit a tuple frequently, you can decompose a tuple into a separable constant or variable:

Let (statuscode,statusmessage) = Http404error

println ("The Status code is \ (StatusCode)")//Print out "The status code is 404"

println ("The Status message \ (StatusMessage)")//Print out "The status message not Found"

If you just need some of the tuple's values, you can use underscores when decomposing tuples to ignore parts of tuples that you don't need:

Let (justthestatuscode,_) = Http404error

println ("The Status code is \ (Juststatuscode)")//can only access Justthestatuscode

Alternatively, a single value in a tuple is accessed through a single lead, which starts at 0:

println ("The Status code is \ (http404error.0)")//Print out "The status code is 404"

println ("The Status message \ (Http404error.1)")//Print out "The status message not Found"

When a tuple is defined and a name is defined for each node, you can also get a value for the specified name by name:

Let Http200status = (statuscode:200, description: "OK")

println ("The Status code is \ (Http200status.statuscode)")//Print out "The status code is 200"

println ("The Status message \ (http200status.description)")//Print out "The status message is OK"

Tuples are often used for the return value of a function. A function needs to return the status of a Web page that can be a tuple (int,string) to describe whether the page return value is successful or failed. It is possible to provide more information about the return value of a function by having a tuple of two different types with different values as the return value of a function than using only one single type of value. For more information, please refer to the "Functions of siwft return value as a tuple" subsection

Note: Tuples are often used for values that need to be temporarily tied together. He is not fit to create some complex data structures. If your data structure life cycle exceeds the temporal range, you will sooner or later use a class or struct-like structure.

Swift's tuple

Related Keywords:
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.