Breeze annotation-Swift programming language: Point11 ~ 15,-swiftpoint11
Directory Index
Breeze annotation-Swift Programming Language
Point 11.
Numeric literal
Code example:
Let decimalInteger = 17 // decimal 17let binaryInteger = 0b10001 // binary 17let octalInteger = 0o21 // 17let hexadecimalInteger of octal = 0x11 // 17 of hexadecimal format
Note:
- Integer literal can be written as follows: decimal number without prefix; binary number, Prefix: 0b; octal number, Prefix: 0o; hexadecimal number, Prefix: 0x.
- The floating point literal can be decimal (without a prefix) or hexadecimal (with a prefix of 0x ).
- A floating point literal must have at least one decimal number (or a hexadecimal number) on both sides of the decimal point ).
- The literal number of a decimal floating point has an optional index, which is specified by e in upper or lower case.
- The hexadecimal floating point literal must contain an index, which is specified by p in upper or lower case.
Let decimalDouble = 17.2e0 // 17.2let hexadecimalDouble = 0x11. 2p0 // 17.125 Of The hexadecimal floating point number
- If the exponent of a decimal number is exp, the number is equivalent to the product of base number and 10 ^ exp.
- If the exponent of a hexadecimal number is exp, the number is equivalent to the product of the base number and 2 ^ exp.
- The literal numbers of integers and floating-point numbers can contain additional formats (0 and underline) to enhance readability. These formats (0 and underline) do not affect the actual literal values.
let paddedDouble = 000123.456let oneMillion = 1_000_000let justOverOneMillion = 1_000_000.000_000_1
Point 12.
Numeric type conversion
Code example:
let twoThousand: UInt16 = 2_000let one: UInt8 = 1let twoThousandAndOne = twoThousand + UInt16(one)
Note:
Point 13.
Type alias
Code example:
Typealias AudioSample = UInt16 // The type alias of UInt16 is defined as AudioSamplevar maxAmplitudeFound = AudioSample. min // maxAmplitudeFound is now 0
Note:
- A type alias defines another name for an existing type.
- Use the typealias keyword to define the type alias.
- After defining a type alias, you can use the alias wherever the original name can be used.
Point 14.
Boolean Value
Code example:
Let orangesAreOrange = true // The value is true, and the value is false.
Note:
- A boolean value is a logical true or false value.
- Swift has two Boolean constants: true and false.
- If a non-Boolean value is used where the Bool type is required, the Swift type Security Mechanism reports an error.
Point 15.
Tuples
Code example:
// The type of http404Error is (Int, String), and the value is (404, "Not Found") let http404Error = (404, "Not Found ")
Note:
- Tuples combine multiple values into a composite value.
- The values in the meta-group can be of any type, but they do not need to be of the same type, and the order of the types is also arbitrary.
- The content of tuples can be divided into separate constants and variables, and then you can use them normally.
Let (statusCode, statusMessage) = http404Error // output "The status code is 404" println ("The status code is \ (statusCode )") // output "The status message is Not Found" println ("The status message is \ (statusMessage )")
- If you only need a portion of the values of the tuples, you can mark the ignored parts with underscores.
Let (justTheStatusCode, _) = http404Error // output "The status code is 404" println ("The status code is \ (justTheStatusCode )")
- You can use subscript to access a single element in a tuples. The subscript starts from scratch.
// Output "The status code is 404" println ("The status code is \ (http404Error. 0) ") // output" The status message is Not Found "println (" The status message is \ (http404Error. 1 )")
- You can name a single element when defining the tuples, and obtain the value of the element through the element name.
Let http200Status = (statusCode: 200, description: "OK") // output "The status code is 200" println ("The status code is \ (http200Status. statusCode) ") // output" The status message is OK "println (" The status message is \ (http200Status. description )")
- Tuples can be used as function return values.
Author: Qingfeng fuliu (DashGeng)
Source: http://www.cnblogs.com/dashgeng/
This blog post is copyrighted by the author. You are welcome to repost it! This statement must be retained without the author's consent, and the original article connection is clearly provided on the article page; otherwise, the right to pursue legal liability will be retained.