Swift learning-Swift BASICS (6)

Source: Internet
Author: User

Optionals Selection


The selection (as if not translated in this way) is applicable to situations where values may be null. There are two options for one selection: The value is equal to or equal to X, or the value does not exist.

The concept of selection is not in OC and C. The closest concept in OC is that the return value in OC is the function of the object. If the object does not exist, nil can be returned, however, nil can only represent objects. It cannot represent basic types, struct types, or enumeration types. In oC, nsnotfound is used to indicate that the value does not exist. In swift, other types are not required to indicate non-existent values.

For example:

In swift, a method of the string type is toint, but not all strings can be converted to int.

let possibleNumber = "123"let convertedNumber = possibleNumber.toInt()// convertedNumber is inferred to be of type "Int?", or "optional Int
Because toint may fail, it will return an optional int type (write Int ?) Instead of int type, it indicates that the returned value can be an int type value or has no value.



If statements and forced unwrapping if Condition Statement and force resolution


You can use the if Condition Statement to determine whether there is a value in an option. Once you confirm that the option contains a value, you can add an exclamation point (!) after the option name (!), It indicates that I know that this option contains values. This process is called mandatory resolution of the option value.

if convertedNumber {    println("\(possibleNumber) has an integer value of \(convertedNumber!)")} else {    println("\(possibleNumber) could not be converted to an integer")}// prints "123 has an integer value of 123
In use! Before that, make sure that the actual value is stored in the selected value, not empty.



Optional binding of optional binding


You can use the optional binding to determine whether a value exists in an optional value. If there is a value, you can use this value as a constant or variable. You can use the if or while statement to determine and assign values.

You can rewrite the above forced resolution and use the optional binding to determine whether an optional value is null:

if let actualNumber = possibleNumber.toInt() {    println("\(possibleNumber) has an integer value of \(actualNumber)")} else {    println("\(possibleNumber) could not be converted to an integer")}// prints "123 has an integer value of 123"
The above process can be understood:

If the optional integer value obtained by possiblenumber. toint contains a value, define a constant named actualnumber to store this value.

If the conversion is successful, the actualnumber is available in the first condition of if. It has been initialized and contains an optional value, which is not required! To access its value.

During the optional binding process, you can use variables or constants.



Nil Null Value


var serverResponseCode: Int? = 404// serverResponseCode contains an actual Int value of 404serverResponseCode = nil// serverResponseCode now contains no value
If no value is assigned when defining constants and variables, the default value is nil.

var surveyAnswer: String?// surveyAnswer is automatically set to nil

Note: nil in swift and nil in OC indicate different meanings. Nil in OC is a pointer that specifies no type object, in swift, Nil is an empty province value of the specified type. The optional value of any type can be set to nil, not necessarily the object type.


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.