Swift inside! And? The role

Source: Internet
Author: User
Tags unpack

Generally we will encounter in the following two kinds of situations! And? The use
1. When declaring a variable

var number:int?

var str:string?

2. When working with variables

Number?. HashValue

Str!. HashValue

Because these two situations have different meanings, they are interpreted separately:

1>: When declaring a variable
When declaring a variable, Swift does not automatically initialize the variable to a default value if it is not initialized manually.

var a:string

var B = A//error: Because there is no initialization a,a no value

However, for optional variables, if the optional variable is not initialized at the time of declaration, Swift automatically initializes the variable to nil. When declaring a variable, add it after the type? Or! is to tell the compiler that this is a optional variable, and if not initialized, you initialize it to nil .

var a:string? A is nil

var b:string? B is nil

var a_test = A//A_test to nil

var b_test = b//b_test to nil

But there are some differences between the two, in detail later.
Optional is actually an enumeration type, as can be seen, optional contains None and Some two types, and nil is optional.none, non-nil is optional.some. If the optional variable is not initialized at the time of declaration, Swift calls Init () to initialize the variable to nil, and when the variable is initialized with a non-nil value, the original value is wrapped by some (T), so we need to unpack the original value in order to use it afterwards.

2. When working with variables

var arraycount = dataList?. Count

At this point the question mark is similar to Isresponsetoselector, that is, if the variable is nil, it cannot respond to the subsequent method, so it returns nil directly. If the variable is non-nil, the package some (T) is removed, and the original value is removed to perform the subsequent operation.

datalist!. Count

The exclamation point here is different from the previous question mark, which indicates that I am sure that DataList must be non-nil, so we can unpack the original value directly and handle it. So if you accidentally let DataList be Nil, the program will crash out.

Back to the above statement? And! The difference between the question up
When declaring a variable? Simply tell Swift that this is optional, and if it is not initialized it defaults to nil, and through! Declaration, then the operation of the variable will be implicitly added before the operation of a!.

Summarize

      1. Question mark?
        A. Add when declaring? , tells the compiler that this is optional, and automatically initializes it to nil if the declaration is not manually initialized
        B. Before the variable value operation is added? To determine if the variable is nil, it does not respond to the subsequent method.
      2. Exclamation mark
        A. Add when declaring! , tell the compiler that this is optional, and then, after the operation of the variable, it is implicitly added before the operation!
        B. Before you add to the variable operation! , which means that the default is non-nil and the package is processed directly

Swift inside! And? The role

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.