Swift Language Guide (vii) Swift Language Foundation: Boolean and type aliases

Source: Internet
Author: User
Tags aliases bool constant min

Boolean value

Swift has a basic Boolean type, called Boolean (bool), and a Boolean value called a logical value (logical), because it can only be true (true) or False (false). Swift provides two Boolean constant values: True,false:

1 Let orangesareorange (oranges are oranges) = True
2 Let turnipsaredelicious (turnip is delicious) = False

Orangesareorange and Turnipsaredelicious are inferred as Boolean types because their values are initialized by the boolean literal. As with the int or double mentioned earlier, it is not necessary to declare bool for a variable or constant, as long as you set its value to TRUE or false at creation time (after the value is true or FALSE, Swift infers its bit bool type--joe.huang). Type inference makes swift code more concise and readable when a value of a known type is initialized to a constant or variable.

Boolean values (bool) are useful when you use a condition such as if to judge a statement:

If turnipsaredelicious {
    println ("Mmm, Tasty turnips!") How delicious the turnip is!
else {
    println ("Eww, turnips are horrible.") Bah, the turnip is awful.
}
Prints "Eww, turnips are horrible.//output" bah bah, turnip is awful. "

Conditional control statements such as the If class are described in the Process Control chapter.

Swift's security type mechanism avoids the use of non Boolean values as Boolean values, and the following example complains at compile time:

1 Let i = 1
2 If I {
3//This example would not compile, and'll is an error
4//This example will not be compiled, and will be an error, because I is int type--joe.huang
5}

The following example is compiled by:

1 Let i = 1
2 if i = = 1 {
3//This example'll compile successfully
4//This example is compiled successfully because I==1 returns True and the return value is Bool--joe.huang
5}

i = = 1 The result type of the comparison is Bool, so the second example can be checked by type. i = = 1 These comparisons are discussed in the basic operator (tentatively translated) chapter.

Like other types of security rules in Swift, these rules avoid accidental bursts of error and ensure that the purpose of each piece of code is always clear.

Type Alias

Type aliases is an alternative name for an existing type (provides an alternative name for an existing type), and a type alias is defined using the keyword Typealias.

Type aliases are useful when you need to provide a more appropriate name for an existing type in the context, such as when you are working with data of a specific width from an external data source:

1 Typealias audiosample (audio sample) = UInt16

Once you have defined the type alias, you can replace the original name of the existing type with the type alias elsewhere:

1 var maxamplitudefound = Audiosample.min
2//Maxamplitudefound is now 0//maxamplitudefound (maximum amplitude) is 0, based on the alias Audiosample defined as 16 in the previous example, Audiosample.min is uint16.min. --joe.huang

Here audiosample is defined as an alias for UInt16. Because it is an alias, the call to Audiosample.min is actually called Uint16.min, which ultimately provides an initial value of 0 for the maxamplitudefound variable.

Author: cnblogs Joe.huang

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.