Swift learning-Swift BASICS (IV)

Source: Internet
Author: User

A: children's programming is not good!

B: most of them do not like learning. Starting from reading the English version, you will fall in love with programming!


Type aliases type redefinition (typedef)

The keyword of the type that is redefined in SWIFT is typealias. As for how to use it, you don't have to talk about it. Let's look at the example:

Typealias audiosample = uint16 // a type name audiosample is defined, representing the uint16 type var maxamplitudefound = audiosample. Min // maxamplitudefound is now 0



Booleans Boolean Type

Boolean has two values, true or false:

let orangesAreOrange = truelet turnipsAreDelicious = false
Haha, the literal meaning of the first constant is: Orange, the answer is true; the literal meaning of the second constant is: radish is delicious, and the answer is false.

It is estimated that the author does not like radishes or American radishes. In fact, there are many Chinese radishes that are delicious. Welcome to China, haha.

Oh, by the way, the original article also said that, unlike Int & Double, When you define a constant or variable as the bool type, if you assign true or false values, so we don't need to declare the bool type. Swift will judge it by itself (WIT ).

Of course, bool is the most commonly used in if statements:

if turnipsAreDelicious {    println("Mmm, tasty turnips!")} else {    println("Eww, turnips are horrible.")}// prints "Eww, turnips are horrible.
Note: In swift, other values are not allowed to replace bool, which means that in the Condition Statement, the condition can only be of the bool type, and Other types (such as 0, 1) it is not allowed. An error will be reported during compilation. It is said that this is done to make swift more secure.

Compilation error:

let i = 1if i {    // this example will not compile, and will report an error}
Compiled version:

let i = 1if i == 1 {    // this example will compile successfully}
Then, the original article explains why Swift does not allow the statement, which is different from other languages: it is still in consideration of security. Writing this statement can avoid unexpected errors, and ensure that the intention of the conditional statement is very clear.



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.