Swift Learning Swift Programming Tour (III)

Source: Internet
Author: User

  Tuples (tuples)

  Tuples is the value of combining multiple single values into one composite. It can contain any type of value, and it does not need to be the same type.

I. Creation of tuples

1.

Let Http404error = (404,"notfound! "  = http404errorprint ("Thestatus code is \ (StatusCode), the status of message is \ ( StatusMessage)")

2.

Let Httperror = (statusCode:404, StatusMessage:"notfound! ")

Output results

Two. access to tuple data

1.statuscode,statusmessage is like a key in a dictionary, which we can use to access the elements of a meta-ancestor.

2. You can also use the subscript to access

Three. The Ignore of tuple elements

Sometimes we don't need all the elements, we can use the _ underline to ignore the unwanted elements .

Let Http404error = (404,"notfound! "  = http404errorprint ("Thestatus code is \ (statusCode)")

Tuples are useful when you return a value as a function. A function used to fetch a Web page might return an (Int, String) tuple to describe whether it was successful. A tuple that contains two different types of values can make the return information of a function more useful when compared to a value that returns only one type.

Optional (Optional)

Use the optional optional to handle some missing value cases.

There is a value, and it equals X has an equal to X
OR OR

There isn ' t a value at all No values

Attention:

There is no alternative to this concept in the C and Objective-c languages. The closest objective-c feature is that a method either returns an object or returns Nil,nil to indicate "a valid object is missing." However, this only works on objects--for structs, the basic C type or enumeration type does not work. For these types, the Objective-c method typically returns a special value (such as nsnotfound) to imply that the value is missing, and Swift's optional allows you to imply that any type of value is missing.

If a type is optional then add it after the type, that is, optional as int?

Let's use a simple example to learn about optional

" 12345 "  = Int (possiblenumber) print (number)

Output results

"Hello,how is You?"  "= Int (possiblenumber) print (number)

Output results

You set a worthless optional variable to nil

If statements and forced parsing

You can use the IF statement to determine whether an optional value is included. If there is an optional value, the result is true, and if there is no value, the result is false.

After you have determined that the optional package does contain a value, you can add an exclamation mark (!) to the optional name. To get the value. This exclamation point means "I know this optional value, please use it." "This is called forced parsing of optional values (forced unwrapping):

  

Swift Learning Swift Programming Tour (III)

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.