Welcome to Swift (initial translation and annotations of Apple's official Swift document 12)---77~83 page (chapter II)

Source: Internet
Author: User

Optionals (optional type)

When a value may have a value, there may also be no value, in which case you can use an optional type. The optional type means:

There is a value that is equal to X, or that there is no value at all.

Note the point:

The concept of an optional type is not available in both C and OC. In the most similar case in OC, a method returns nil or returns an object. The return nil in OC means that there are no legitimate objects. However, this can only be for objects, not for structs, for basic data types, or for enumeration values. For these types, OC methods can only return a fixed value (such as Nsnotfound) To indicate that there is no value. This is where the caller of the method clearly remembers to return the fixed value for testing and checking. In Swift, an optional type can be used for any type of value and no longer requires a fixed constant.

For example, in Swift, a string type has a method called ToInt, which converts a string value to an int value, but not every string value can be converted to an int value. A string of type "123" can be converted to 123 of type int, but is a string of type ' Hello,world ' but cannot be converted to a value of any int type.

The following code uses the ToInt method to attempt to convert a sting type to an int:

Let Possiblenumber = "123"

Let Convertednumber = Possiblenumber.toint ()

Convertednumber is inferred to being of type "int?", or "optional int"

Because the ToInt method may fail, it returns an optional type int instead of an int. Optional type int is an int, not an int. Question mark? indicates that it is an optional type value, which means that it may have some value of type int, or it does not have any value at all ( It also cannot contain other types of values, such as a Boolean or string value, which is either an int or nothing.)

If statements and forced unwrapping (if statement and forced unpacking)

You can use the IF statement to confirm that an optional type has a value. If an optional type has a value, it is equivalent to true if it has no value, which is equivalent to false.

Once you have determined that the optional type has a value, you can add an exclamation point (!) after the optional type name. To access the internal value, the exclamation point means: "I know this optional type has a value, please use this value." This is the forced unpacking of the so-called optional type value.

If Convertednumber {

println ("\ (possiblenumber) have an integer value of \ (convertednumber!)")

} else {

println ("\ (Possiblenumber) could not being converted to an integer")

}

Prints "123 have an integer value of 123"

For more if statements, refer to Process Control flow.

Note the point:

Trying to use an exclamation point (!) Accessing an optional type that does not have a value will trigger the run-time error. When you use the forced unpacking optional type, be sure to verify that it has a value.

Optional binding (optional binding)

An optional binding allows you to determine whether an optional type contains a value, and generates a temporary constant or variable to hold the value. Optional bindings are often used with the IF and while statements to detect the values contained by an optional type.

You can use an optional binding in the IF statement, as in the following code:

If let Constantname = someoptional {

Statements

}

In the following code, you can also use optional bindings to override the Possiblenumber example without forcing the package:

If Let Actualnumber = Possiblenumber.toint () {

println ("\ (possiblenumber) have an integer value of \ (Actualnumber)")

} else {

println ("\ (Possiblenumber) could not being converted to an integer")

}

Prints "123 have an integer value of 123"

The above code can be understood as:

"If the int optional type returned by Possiblenumber.toint contains a value, assign the value to a constant called actualnumber"

If the condition is true, the constant actualnumber will have a value in the first branch of the IF statement, which is the value contained within the optional type, and it initializes the constant actualnumber with it. Therefore no longer need to use an exclamation point! In the code sample above, Actualnumber is only used to print out this result.

Optional bindings can also be used with constants or variables. If you want to manipulate the value of Actualnumber in the first branch of the IF, you can use if Var actualnumber instead, so that the value contained in the optional type will be assigned to the variable instead of the constant.

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.