Swift 2.0 Study Notes (Day 28)-summarize the use of question marks (?) and exclamation points (!)

Source: Internet
Author: User

original articles, welcome reprint. Reprint Please specify: Dongsheng's blog

When you use optional types and optional chains, you use the question mark multiple times (? ) and an exclamation point ( ! ), but their meaning is different, let me explain in detail below.  

1. question mark (?) in the optional type. )

declares that this type is an optional type and uses an exclamation mark when accessing a variable or constant of this type ( ! ), the following code shows the split package:

Let result1 : Double? = Divide (+)

Print (result1!)  

2. an exclamation mark in an optional type (! )

declaring this type is optional, but you can access variables or constants of this type without using an exclamation point ( ! ), the following code is an implicit unpacking:

Let result3 : Double ! = Divide (+)

Print (RESULT3)

3. an exclamation point in the optional chain (! )

Multiple instances have an association, and a reference chain is formed when a member of another instance's methods, properties, and subscripts are referenced from one instance, and since some links in this "chain" may have values or no values, they need to be accessed in the following way:

Emp.dept!.comp!.name

4. question mark (?) in the optional chain. )

use an exclamation mark in an optional chain ( ! ), once the "chain" has no value, the program will be abnormal, we put the exclamation mark ( ! ) Instead of a question mark ( ? ), the code looks like this:

Emp.dept?.comp?.name

so that some links do not have value when the return Nil , the program does not have an exception.

Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom

More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php

Swift 2.0 Study Notes (Day 28)-summarize the use of question marks (?) and exclamation points (!)

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.