Spy on Swift's new addition data type tuples and optional values

Source: Internet
Author: User

Today's blog summarizes some of the two new data types added in Swift relative to OBJC: tuple (tuple) and optional value type (Optional). The above two types are unique to Swift, and are very handy to use today, with examples that describe how to initialize and use tuples and optional value types, and scenarios that use tuples and optional value types. Talk less, go straight to the topic, and still test using playground in today's sample code. The left is the code, and the right side is the result in playground.

I. Tuples in swift (tuple)

A tuple is similar to a struct (struct) in C, which stores a set of related values and allows the types of these related values to be different. Tuples are typically used when the array returns multiple values.

1. Define tuples directly, without specifying the type of elements in the tuple. The syntax for defining tuples is to use a pair of parentheses directly, with each value separated by commas in parentheses. The following are the specific examples:

2. Remove the values in the tuple, one: directly assigned to the corresponding constant (or variable), the following line of code is to directly assign the tuple to the constant, in swift, the connection between the strings can use the + number operator. Passing the following statement assigns the value of the tuple to our defined constants once. A word summed up: "A radish a pit, the seat."

3. You can also get the value of an element by the subscript index of the tuple, which is zero-based, and below the tuple's subscript to get the element value of the tuple.

4. We can also explicitly specify the type of each element of the tuple, which specifies the data type for each element of the tuple student1, the first two are the string type, and the latter two are integral types. If you try to assign integer data to an element of the string type, the compiler will give an error and prompt for the data type specified by each element of the tuple.

5. Assigning the correct value to each element of the tuple will show the value that each element should have on the playground.

Ii. optional type in Swift (Optional)

The optional type is the new type in swift, the optional type makes Swift's data type more secure, the optional type can be said to play an important role in Swift, it makes your code more robust, the following will give the optional type of usage scenarios. An optional value, explained by plain English, is not sure if the variable or constant has a value (possibly nil), so the value of this variable or constant is optional. The optional is described in detail below.

1. Optional value type can be directly assigned to nil, for type-safe and normal variables and constants are not directly assignable to the value of nil as follows:

2. The syntax for defining an optional value type is to add a number after the type declaration, stating that the value of the variable is indeterminate, so you add one? No. The following is to assign a nil to the optional value type, will not error.

3. After assigning a value to an optional type variable, if you want to use the value of the optional variable, you need to force open with an exclamation point (!) and the compiler will give an error if it is forced to open without an exclamation point (!). Why force the value of an optional variable to be opened with an exclamation mark? The reason is simple because the value of the optional type may be nil, the reason for forcing open with an exclamation point (!) is to tell the compiler that "the value of this optional variable is not empty, I want to use it", and when the value of the optional variable is forced to open, the compiler will not give an error.

The following optionalvalue is an optional type of variable and assigns "Ludashi" to it, if not used! Forced to open the value of Optionalvalue, compile will throw red error.

4. Take a look at the usage scenarios for the optional types and use small instances.

(1) When converting a string to an integer, we are going to accept the variable (or constant) of the optional type, because we are not sure if the conversion succeeds, and then after the non-null judgment, the exclamation point (!) is used to force the open.

The following converts a numeric string into an integer, then uses a variable to receive it, then determines whether the variable is empty, is forced to open for use without being empty, and is forced to open and use after the conversion is not nil.

      

The following is the case of nil after conversion, will not do processing, because the judgment is nil, so do not do the processing.

(2) When you use a method of a class, you are not sure if the class has this method, can you use it? To open the optional value, and then call the method of the class. The number is to tell the compiler that you don't know if the class has the method to call, and if there is one, ignore it. As shown in the following example:

If Numberinteger is nil, IsEmpty will not be called, and Resultempty will be assigned nil, as shown below.

The use of tuples and optional types also has to be combined with specific application scenarios in the project to make it more effective, this blog first through a few small examples to see the use of tuples and optional types and usage scenarios, extrapolate is the most important. Today's blog is here.

Spy on Swift's new addition data type tuples and optional values

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.