Swift Learning notes-implicitly unwrapped Optionals

Source: Internet
Author: User

Implicitly unwrapped Optionals:

An implicitly unwrapped optional variable

varmaybestring:string!maybestring=nilmaybestring= "Fish"

Methods invoked directly, failing at runtime//if the optional is nil

Ifmaybestring.hasprefix ("F") {

println ("maybestring starts with ' F '")

}else{

println ("Maybestring does not start with an ' F '")

}

Tips:

? Implicitly unwrapped optionals is useful in classes and structs where you won ' t

There is a property's value available in the initializer, but would have a value later

Before it is used.

? Remember, implicitly unwrapped optionals is still optionals–use with them with

Care since they remove a bit of safety around nil values!

Implicit parsing optional

As mentioned above, optional implies that a constant or variable can "have no value". Optionally, the IF statement can be used to determine if there is a value, and if there is a value, the value can be resolved by an optional binding.

Sometimes in the program architecture, after the first assignment, you can determine that an optional always has a value. In this case, it is very inefficient to judge and parse an optional value every time, because you can be sure that it always has a value.

This type of optional is defined as an implicit parsing option (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, refer to cyclic strong references between class instances.

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

Let possiblestring:string? = "An optional string."

println (possiblestring!) An exclamation point is required to get the value

Output "an optional string."

Let assumedstring:string! = "An implicitly unwrapped optional string."

println (assumedstring)//No exclamation mark required

Output "an implicitly unwrapped optional string."

You can choose implicit parsing as an optional option that can be parsed automatically. 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: A run-time error is triggered if you try to take a value when you implicitly parse an optional no value. And you add an exclamation point to the normal optional value.

You can still choose implicit parsing as a normal alternative to determine if it contains a value:

If assumedstring {

println (assumedstring)

}

Output "an implicitly unwrapped optional string."

You can also use implicit parsing in an optional binding to check and resolve its value:

If let definitestring = assumedstring {

println (definitestring)

}

Output "an implicitly unwrapped optional string."

Note: Do not use implicit parsing if a variable may become nil after it is selected. Use a normal optional type if you need to determine whether it is nil in the lifetime of the variable.

Swift Learning notes-implicitly unwrapped Optionals

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.