Different syntaxes from C/C ++ and Java in Swift (2), swiftjava

Source: Internet
Author: User
Tags error status code

Different syntaxes from C/C ++ and Java in Swift (2), swiftjava

This part focuses on the new optional type (optionals) in Swift)

I. Summary

Optional is a new type of Swift innovation. First, let's take a look at the optional application scenarios:

Var errorCode: Int = 404

At this time, the errorCode value is 404, indicating that the error status code is 404. When an error is generated, the corresponding errorCode is obtained,

But what should I do if there are no errors?

In C/C ++ and Java, We will assign a value of 0 or null to errorCode (in fact, null is 0 ), but how can we ensure that 0 is not a wrong status code?

 

To solve this problem, Swift provides the optional type (optionals). When this value does not exist, we use nil to represent it,

Note that:

Var errorCode: Int = 404

ErrorCode = nil

This is incorrect because nil is a special type and cannot be assigned to int variables.

The correct operation is:

Var errorCode: Int? = 404

ErrorCode = nil

Add one? That's right. At this time, Int? Indicates an optional Integer type.

Note that the optional type cannot be assigned to variables that cannot be selected.

 

2. Optional unpacking

When you use an optional type, you need to unpack the package. There are two unwrap methods:

1) Forcibly unpack

Var errorCode: Int? = 404

Print ("The errorCode is" + errorCode !)

This method requires developers to ensure that the value of errorCode is not nil, otherwise an error will be reported.

2) if-let unpack

Var errorCode: Int? = 404

If let errorCode = errorCode {

Print ("The errorCode is" + errorCode)

}

We recommend that you use this method. When multiple volumes need to be unwrapped:

If let errorCode = errorCode,

ErrorMessage = errorMessage {

}

 

Iii. optional Chaining

Let's take a look at the application environment:

Var errorMessage: String? = "Not Found"

If let errorMessage = errorMessage {

ErrorMessage. uppercaseString

}

This method is incorrect because errorMessage is a String instead of a String?

If you want to use the String uppercaseString method, you can do this:

ErrorMessage ?. UppercaseString

Or

ErrorMessage !. UppercaseString (determined not to be nil)

The returned type is also optional.

If errorMessage !. UppercaseString is returned, and then the operation is similar to a chain.

 

Iv. nil coalesce

We recommend that you assign an initial value to the variable when declaring the variable. However, sometimes we want to assign values to the variable after making a logical decision.

At this time, we can do this:

Var errorMessage: String? = Nil

Let message = errorMessage = nil? "Not Found": errorMessage

Of course, you can use nil coalesce

Var errorMessage: String? = Nil

Let message = errorMessage ?? "Not Found"

 

5. Optional tuples

Var error1: (errorCode: Int ?, ErrorMessage: String ?) = (404, "Not Found ")

ErrorCode and errorMessage are optional, but error1 is not optional.

Therefore, errorCode and errorMessage can be nil, but error1 cannot.

Var error1: (errorCode: Int ?, ErrorMessage: String ?)? = (404, "Not Found ")

ErrorCode and errorMessage. error1 is optional.

 

6. Implicit Type Selection

Mainly used in function Constructor

Var errorMessage: String! = Nil

! During use, you do not need to unpack the package, but you must ensure that it is not nil.

The constructor can be defined in this way, but it will assign values before use.

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.