Wind notes-swift programming language: point11~15

Source: Internet
Author: User
Tags ranges

Catalog Index

Breeze Annotated-swift programming language

Point 11.

Numeric type literal

code Example:

 -           // decimalBinaryinteger = 0b10001       //  binaryoctalinteger = 0o21           // Eight- binary 0x11     // 16 binary

Annotations:

  • The integer number of polygons can be written: decimal number, no prefix, binary number, prefix is 0b, octal number, prefix is 0o, hexadecimal number, prefix is 0x.
  • The floating-point literal can be either decimal (without a prefix) or hexadecimal (the prefix is 0x).
  • Floating point literals must have at least one decimal digit (or hexadecimal number) on either side of the decimal point.
  • Decimal floating-point literals have an optional exponent, specified by uppercase or lowercase e.
  • Hexadecimal floating-point literals must contain exponents, specified by uppercase or lowercase p.
  • 17.2e0        // 17.2 of decimal floating-point numbers 0x11. 2p0  //  hex Floating-point 17.125
  • If the exponent of a decimal number is exp, that number is equal to the product of cardinality and 10^exp.
  • If the exponent of a hexadecimal number is exp, that number is equal to the product of cardinality and 2^exp.
  • Integer and floating-point literals can include additional formatting (0 and underscore) to enhance readability, and these formats (0 and underscore) do not affect the actual value of the literal.
  • 000123.456== 1_000_000.000_000_1

Point 12.

Numeric type conversions

code Example:

Let twothousand:uint16 =1= Twothousand + UInt16 (one)  

Annotations:

  • In general, use the Int type, even if the integer constants and variables in your code are known to be non-negative.
  • Using the default integer type guarantees that integer constants and variables can be reused directly, and can match the type inference of integer class literals.
  • Using a type with an explicit length specifies that a value overflow can be found in time and implies that special data is being processed.
  • Variables and constants of different integer types can store numbers of different ranges.
  • If the number exceeds the constant or variable can be stored in the range, the compile time will be error.
  • Because each integer type can store different ranges of values, you must selectively use numeric type conversions, depending on the situation.
  • By selectively using type conversions, you can prevent implicit conversion errors and make the type conversion intent in your code clear.
  • To convert one number type to another, use the current value to initialize a new number of the desired type, which is the type of the target.
  • The conversion of integers and floating-point numbers must explicitly specify a type.
  • 30.14159= Double (three) + Pointonefouronefivenine
  • When a floating-point number is converted to an integer, floating-point values (fractional parts) are truncated.

Point 13.

Type aliases

code Example:

Typealias audiosample = UInt16          // the UInt16 type alias is defined as Audiosample // Maxamplitudefound is now 0  .  

Annotations:

    • A type alias is one that defines another name for an existing type.
    • Use the Typealias keyword to define the type alias.
    • Once you have defined a type alias, you can use the alias anywhere you can use the original name.

Point 14.

Boolean value

code Example:

  True     // value is True false // value is False   

Annotations:

    • Boolean values are logical true or FALSE.
    • Swift has two Boolean constants, True and false.
    • If a non-Boolean value is used where the Bool type is required, the type safety mechanism of Swift will error.

Point 15.

Meta-group

code Example:

the type of //  Http404error is (Int, String), the value is (404, "not Found")let Http404error = (404" Not Found ")  

Annotations:

  • Tuples combine multiple values into a single composite value.
  • The values within the tuple can be any type, do not require the same type, and the order of the types is arbitrary.
  • The contents of a tuple can be decomposed 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 ("Thestatus message is \ (statusmessage)")
  • If you only need a subset of the tuple values, you can use the underline (_) to mark the part you want to omit when decomposing.
  • Let (Justthestatuscode, _) = Http404error//  output "The status code is 404"println ("  The status code is \ (justthestatuscode)")
  • A single element in a tuple can be accessed by subscript, with the subscript starting from zero.
  • // output "The status code is 404"println ("Thestatus code is \ (http404error.0)") // output "The status message is not Found"println ("Thestatus message is \ (Http404error.1)" /c11>)
  • You can name a single element when you define a tuple, and you can get the value of an element by naming it.
  •  $ " OK " )//  output "The status codeis a" println ("Thestatus code is \ ( Http200status.statuscode)//  output "The status message is OK"println ( " The status message is \ (http200status.description) ")
  • Tuples can be used as return values for functions.

The Breeze to Touch Willow (Dashgeng)

Source: http://www.cnblogs.com/dashgeng/

This article starts the blog garden, the copyright belongs to the author all, welcome reprint! This statement must be retained without the consent of the author, and the original text is connected in a prominent position on the article page.Otherwise, the right to pursue legal liability is retained.

Wind notes-swift programming language: point11~15

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.