Optional type in swift (optional type) with forced unpacking?!

Source: Internet
Author: User

How often do we meet in the development of Swift? And! , understanding the deep content of these two symbols is very advantageous to our development;

At present, the online tutorial on swift3.0 is quite small, if you go to search will find early statements, when defining variables, Swift will not assign the value of the initial value of variables, so when you declare a string variable, var string:string will be the system error, But the current swift version does not have an error.

But if you want to assign a string to a value that is not nil, var string:string = nil must be wrong because nil is not a string type, and in fact he is optional.none, the correct notation should be var string:string? = nil;

? Represents an optional type in Swift to handle the absence of a value, optionally indicating that there is a value, and that the value can be x or null;

! Represents force unpacking, which is an implicit mandatory optional type, which means that the variable of the current tag must not be empty and, of course, it will crash if it is empty. Either the variable type of the current tag and the type of assignment are consistent.

Optional binding (optional binding)

Use the optional binding to determine if the optional type has a value, and assign the value to a constant or temporary variable if there is a value.

The optional binding can be used in the IF and while statements:

var string:string? = "E" if let Test = string {    print ("Variable with current optional type has value")}

implicitly resolves an optional type (implicitly unwrapped optionals)

Sometimes in a program, a optional type is assigned for the first time, so you can be sure that it will always have a value.

In this case, it is very inefficient to use it every time you need to determine the optional type of the package, because you can be sure that it always has values,

Therefore, in swift, an implicit parsing optional type is defined, which can be parsed automatically when it is used;

Put (String?) Change to exclamation point (string!) You can declare an implicitly resolved optional type.

Let possiblestring:string? = "An optional string." Let forcedstring:string = possiblestring! An exclamation point is required to get the value let assumedstring:string! = "An implicitly unwrapped optional string." Let implicitstring:string = assumedstring//No exclamation mark required

Swift 3.0 Changes

swift3.0 the optional type control is more rigorous, the implicit optional type and other types of operations are obtained after an optional type rather than an implicit optional type .

Let a:int! = 1let B = A + 1//force unpacking at this time, B is int type let c = A//Note at this time c is int? In the previous Swift version, C was int!

 

Optional type in swift (optional type) with forced unpacking?!

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.