Swift Optionals-1

Source: Internet
Author: User

Learning is a gradual process, and it is difficult to start with everything. Swift says it's easy to get started, but there's a little bit of knowledge that you can learn and practice to understand.

Need to practise a IoT and test what's optiontal Type and how to use it very well

Optional Introduction:

An optional value either contains a value or contains nil (no value) to indicate, a value is missing. Write a question mark (?) After the type of a value to mark the value as optional.

If a var or constant could be a nil, the Var must is declared as optional

Optional is an enumeration

Optional Example:

1. Pnumber is a string constant

2. When cast it to a int, it might is a int or nil since some strings can not casted, liked "good"

3. Optional Binding statement is if let A = b {}

If You is sure the optional does contain a value, it can be accessed by pnumber! (implicitly optional)

Sample Code:

Let Pnumber = "123"

Let Converternumber = Int (possiblenumber!)

If Let Actualnumber = Int (pnumber) {//casting and force it to a constant. A Constan

Print ("\ ' \ (pnumber) \ ' Have an integer valuel of \ (Actualnumber)")

} else {

Print ("\ ' \ (pnumber) \ ' could not is converted to an integer")

}

Swift Optionals-1

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.