The syntax differs from C + + and Java in the Swift language (ii)

Source: Internet
Author: User

In this section, the main focus is on the innovative selection of Swift (Optionals)

I. Summary

Selectable is a new type of swift innovation, first look at the optional application scenario:

var errorcode:int = 404

At this point the value of ErrorCode is 404, indicating that the error status code at this time is 404, when an error occurs, it will get its corresponding errorcode,

But what if there were no errors?

In C + + and Java we give ErrorCode a value of 0 or null (in fact null is 0), but how can we guarantee that 0 is not a wrong status code?

In order to solve this problem, Swift provides a selectable (optionals) When this value does not exist, we use nil to indicate that

It is important to note that:

var errorcode:int = 404

ErrorCode = Nil

This is wrong, because nil is a special type and is not assignable to a variable of type int.

The correct operation is:

var errorcode:int? = 404

ErrorCode = Nil

Add one? That's right, this time int? Represents an integral type of selectable

It is important to note that the selection can not be assigned to the non-selection of variables.

Second, can choose the solution package

When using the optional, the unpacking operation is required and there are two kinds of unpacking (unwrap) modes:

1) Forced unpacking

var errorcode:int? = 404

Print ("The ErrorCode is" +errorcode! )

This unpacking method requires the developer to ensure that the value of errorcode is not nil, otherwise it will be an error.

2) If-let Unpacking

var errorcode:int? = 404

If let ErrorCode = errorCode {

Print ("The ErrorCode is" +errorcode)

}

This is the recommended way to do this when multiple volumes need to be unpacked:

If let ErrorCode = ErrorCode,

ErrorMessage = errormessage{

}

Third, optional Chaining

First look at the application environment:

var errormessage:string? = "Not Found"

If Let errormessage = errormessage{

Errormessage.uppercasestring

}

This is wrong, because errormessage is a string instead of a string?

You can do this if you want to use the Uppercasestring method of string:

Errormessage?.uppercasestring

Or

Errormessage!.uppercasestring (ok not nil)

The return type here is also a selectable

If Errormessage!.uppercasestring returns, then the operation is similar to a chain.

Iv. Nil Coalesce

We recommend assigning an initial value to a variable when declaring a variable, but sometimes we want to assign the variable after the logical judgment that follows it.

We can do this at this time:

var errormessage:string? = Nil

Let message = ErrorMessage = = Nil? "Not Found": errormessage

Of course, by Nil Coalesce way

var errormessage:string? = Nil

Let message = errormessage?? "Not Found"

Five, the meta-group of the optional

var Error1: (Errorcode:int? , errormessage:string? ) = (404, "not Found")

Among them, errorcode and errormessage are selectable, but Error1 is not available for selection

So errorcode and errormessage can be nil, and Error1 can not

var Error1: (Errorcode:int? , errormessage:string? )? = (404, "not Found")

ErrorCode and Errormessage,error1 are all available as a choice.

Six, implicit type can choose

The main application is in the constructor of the function

var errormessage:string! = Nil

! Do not unpack when you use it, but you must ensure that it is not nil

When used, constructors can be defined in this way, but they are then assigned and used later.

The syntax differs from C + + and Java in the Swift language (ii)

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.