Swift Learning--swift explains the specific basis (vii)

Source: Internet
Author: User

Implicitly unwrapped optionals implicit parsing options

Mentioned above. Optional means that the constant or variable "has no value". The optional if declaration is used to infer whether a value exists, assuming that there is a value for the analysis value.

Sometimes in the program architecture, the first time is assigned a value. The ability to determine an optional always has a value.

In such a case. It is inefficient to infer and parse an optional value every time, because it is possible to determine that it always has values.

The optional of such a type is defined as an implicit parsing optional (implicitly unwrapped optionals).

Declare an implicit parsing option by changing the question mark (String?) that you want to use as an optional type to an exclamation point (string!).

Implicit parsing is useful when it is possible to determine after the first assignment that the value is always there. Implicit parsing is primarily used in the construction of classes in Swift, please refer to cyclic strong references between class instances.

An implicit parsing option is, in fact, a common option, but can be used as a non-optional and does not require parsing to get an optional value every time. The following example shows the difference between an optional string and an implicitly resolved optional string:

Let possiblestring:string?

= "An optional string." println (possiblestring!)//requires an exclamation mark to access its value//prints ' an optional string. ' Let Assumedstr ing:string! = "An implicitly unwrapped optional string." println (assumedstring) //No exclamation mark are needed to access their value//prints "an implicitly unwrapped optional String.


You can choose implicit parsing as an option that can be parsed on its own initiative.

All you have to do is declare the exclamation point at the end of the type, not at the end of the optional name each time the value is taken.

Note: Suppose you try to take a value when implicit parsing is optional without a value. Will trigger an execution-time error. And you add an exclamation point to the normal optional value.


You can still infer that implicit parsing is optional as a normal alternative to whether it includes values:

If assumedstring {    println (assumedstring)}//prints "an implicitly unwrapped optional string.
You can also use implicit parsing in an optional binding to examine and parse its value:
If let definitestring = assumedstring {    println (definitestring)}//prints "an implicitly unwrapped optional string.

Note: Do not use the implicit parsing option if a variable may become nil after that. Suppose you need to infer whether the life cycle of a variable is nil if. Please use a normal optional type.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Swift Learning--swift explains the specific basis (vii)

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.