The understanding and use of Swift Optional Type (question mark, and exclamation point!)

Source: Internet
Author: User

Two weeks to fix iOS, that's bragging. Hehe, but after a few days of learning or think iOS is very familiar with some features of JavaScript, there are some Java features, seemingly swift set everyone into a language, I still prefer to write Android, but compared to iOS, Swift seems a lot simpler, animations, controls, screen fitting these are all comparable to that of HA. Today, when I was learning iOS, I encountered an exclamation point (! ) Question mark (? ) problem. More tangled, don't know what this means. So take a look at the documentation and see what it means.
Look at two pictures first


Code hint string required? Or an exclamation mark,


function return value hit a question mark

There is a picture can be proven, not deceptive oh.
In fact, the question mark and exclamation point is a optional type, as the name implies is an optional Variant.
In Java we declare that variables are generally initialized, otherwise there may be errors with null pointers.
When declaring a variable in swift, the variable has a value and may not have a value, so Swift writes

var optionalName:String?

The defined optionalname is a controllable type that allows it to have values and no values.

varas! GalleryViewController

The exclamation mark is used here to indicate that the controller type is deterministic, and it is clear that the type must be determined when a strong transfer is made.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The understanding and use of Swift Optional Type (question mark, and exclamation point!)

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.