Swift language guide (7)-Boolean value and type alias based on language

Source: Internet
Author: User

 

Boolean Value

 

Swift has a basic Boolean Type called boolean (Bool), Boolean value, also known as logical value (Logical), Because it can only be true (True) Or false (False). Swift provides two Boolean constants:True,False:

1 let orangesAreOrange (orange is orange) = true2 let turnipsAreDelicious (Wuhu is delicious) = false

OrangesAreOrangeAndTurnipsAreDeliciousIt is inferred to be of the boolean type because their values are initialized by the Boolean literal. As mentioned aboveIntOrDoubleSimilarly, you do not need to specifically declare variables or constantsBoolAs long as you set the valueTrueOrFalse(The value isTrueOrFalseSwift will infer its locationBoolType -- Joe. Huang ). When a value of a known type is initialized as a constant or variable, type inference makes Swift code more concise and readable.

When you useIfBoolean value (Bool) Is very useful:

1 if turnipsAreDelicious {2 println ("Mmm, tasty turnips! ") // It's delicious! 3} else {4 println ("Eww, turnips are horrible. ") // too difficult. 5} 6 // prints "Eww, turnips are horrible. // output "too many characters, too difficult to eat."

For Conditional Control statements such as the if class, see the process control chapter.

Swift's security type mechanism can prevent non-boolean values from being used as boolean values. The following example reports an error during compilation:

1 let I = 12 if I {3 // this example will not compile, and will report an error4 // this example will not be compiled and an error will be reported, because I is Int type -- Joe. huang5}

The following example is compiled:

1 let I = 12 if I = 1 {3 // this example will compile successfully4 // this example is compiled successfully, Because I = 1 returns true, the returned value is bool -- Joe. huang5}

I = 1 The result type of the comparison is Bool, so the second example can pass the type test. I = 1 this type of comparison is discussed in the basic operators (not translated yet) chapter.

Like other types of security rules in Swift, these rules can avoid unexpected errors and ensure that the purpose of each segment of code is always clear.

 

Type alias

 

Type alias (Type aliasesIs an alternative name of an existing type (providing an alternative name for an existing type), using the keywordTypealiasDefines a type alias.

A type alias is useful when you need to provide a more appropriate name for an existing type in the context. For example, when processing data of a specific width from an external data source:

1 typealias AudioSample (audio sampling) = UInt16

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

1 var maxAmplitudeFound = AudioSample. min2 // maxAmplitudeFound is now 0 // maxAmplitudeFound (maximum amplitude) is now 0. After the AudioSample is defined as a hexadecimal alias in the preceding example, AudioSample. min is UInt16.min. -- Joe. Huang

HereAudioSampleAsUInt16Alias definition. Because it is an aliasAudioSample. minIs actually calledUInt16.min, EventuallyMaxAmplitudeFoundThe variable provides an initial value of 0.

 

Thank you, Swifter-QQ group: 362232993 ~

Fork: https://github.com/Joejo/Swift-lesson-for-chinese

 

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.