Breeze annotation-Swift programming language: Point11 ~ 15,-swiftpoint11

Source: Internet
Author: User

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:

  • Generally, even if the Integer constants and variables in the Code are known to be non-negative, use the Int type.
  • By using the default Integer type, you can ensure that the Integer constants and variables can be reused directly, and can match the type inference of the integer literal.
  • When explicitly specified data types are used, value overflow can be detected in a timely manner and special data is being processed.
  • Variables and constants of different integer types can store numbers of different ranges.
  • If the number is out of the storage range of constants or variables, an error is reported during compilation.
  • Since each integer type can store values of different ranges, You must select numeric type conversion based on different situations.
  • The type conversion method can be used to prevent implicit conversion errors and clarify the type conversion intent in your code.
  • To convert one numeric type to another, use the current value to initialize a new expected numeric type, which is the target type.
  • The type must be explicitly specified for integer and floating-point conversions.
  • let three = 3let pointOneFourOneFiveNine = 0.14159let pi = Double(three) + pointOneFourOneFiveNine
  • When a floating point is converted to an integer, the floating point value (decimal part) is truncated.

 

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.

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.