Optional values for swift (optional)

Source: Internet
Author: User

Apple that document wrote a lot of it and did not write down the optional value (optional) this thing. There is a "Optional Chaining" chapter, but it is not fully explained. Finally found a half-day in the "The Basics" ink a few words. If you don't find optional, you may have missed a very important thing, a variable of non-optional type, and the value of this variable cannot be nil. This differs greatly from OBJC and other programming languages such as C # and Java.

var example:string = "Hello World" example = Nil//error, Type ' String ' does not conform to protocol ' NILLITERALCONVERTIB Le

The value of optional is either nullable or not empty. Define the way it looks like:

var example:string? = "Hello World"

Yes, that's the question mark at the back of the type. With this question mark, this variable is optional. Its value can be nil. However, you cannot directly access this variable.

The non-optional variable cannot be assigned a nil character. Make programming safe. Because, if you do not check whether it is empty, the direct use is compiled. Such as:

var example:string? = "Hello world"//example = Nilvar example2:string = Example    //error, value of optional type ' String? ' not unwrapped

The wrong hint is that a variable of type optional is not "unpacking". So, how to split the package. It's simple, just add an exclamation point to the back, like this:

var example:string? = "Hello world"//example = Nilvar example2:string = example!

“! "Exclamation mark Unwrapp, unpacking optional variable. If the variable has a value, it returns the value, and without it, a run-time error occurs.

So, Apple recommends a more secure way to use optional type variables. First set up an Employee class. It has a member property of name.

Class Employee {    var name:string        init (name:string) {        self.name = name    }}

Then, initialize an employee instance and set it to be optional: var e:employee? = Employee(name: "Tom"). This time if the direct access to the Name property will be an error,e.name = "Tom Cat"//error. as stated above, unpacking is required. e!. Name = "Tom Cat". So what's the safe way to do that:

var e:employee? = Employee ("Tom") if let en = e?. Name {    println ("employee's name is \ (en)")}else{    println ("Unable to identify the employee")}

Access may run-time errors directly using the exclamation point method, such as: leten = e!. Name.

Extended reading: "? "Question mark and"! "Exclamation mark. is a shorthand for optional<t>. Learn more about Swift's generics and optional<t>.

Optional values for swift (optional)

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.