Swift learning-Swift BASICS (7)

Source: Internet
Author: User

Implicitly unwrapped optionals implicit parsing optional

As mentioned above, optional implies that a constant or variable can "have no value ". Optional, you can use the if statement to determine whether there is a value. If there is a value, you can use the optional binding to parse the value.

Sometimes in the program architecture, after being assigned a value for the first time, you can determine that an optional value will always exist. In this case, it is very inefficient to judge and parse the optional values every time, because it can be determined that there will always be a value.

This type of optional is defined as implicit parsing optional (implicitly unwrapped optionals ). Use the question mark (string?) next to an optional type ?) Change to exclamation point (string !) To declare an implicit parsing.

Implicit Parsing is useful when values are always available after being assigned for the first time. Implicit Parsing is optional mainly used in the construction of classes in swift. For more information, see Circular strong references between class instances.

An implicit parsing option is actually a common option, but it can be used as an optional option. You do not need to use parsing every time to obtain the optional value. The following example shows the differences between optional strings and optional strings for implicit parsing:

let possibleString: String? = "An optional string."println(possibleString!) // requires an exclamation mark to access its value// prints "An optional string." let assumedString: String! = "An implicitly unwrapped optional string."println(assumedString)  // no exclamation mark is needed to access its value// prints "An implicitly unwrapped optional string.

You can use implicit parsing as an optional option for automatic parsing. All you have to do is place the exclamation point at the end of the Type when declaring it, instead of the end of the optional name for each value.

Note: If you try to set a value when no value is available for implicit parsing, a running error is triggered. It is the same as adding an exclamation point after a normal option with no value.


You can still use the implicit parsing option as a normal option to determine whether it contains values:

if assumedString {    println(assumedString)}// prints "An implicitly unwrapped optional string.
You can also use the implicit resolution option in the optional binding to check and parse its value:
if let definiteString = assumedString {    println(definiteString)}// prints "An implicitly unwrapped optional string.

Note: If a variable may become nil, do not use implicit parsing. If you need to determine whether it is nil in the life cycle of the variable, use the normal optional type.

Swift learning-Swift BASICS (7)

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.