04_SWIFT2 base type safety and type inference + literal + type Alias

Source: Internet
Author: User

1. Type safety and type speculation1> type safety

Swift is the language of type safe_. A type-safe language gives you a clear idea of the type of value the code will handle . If your code requires a ' String ', you can never accidentally pass in an ' Int '.

Because Swift is type-safe, it makes type checking (type Checks)_ When compiling your code, and marks the mismatched type as an error . This allows you to find and fix bugs early in the development.

When you are dealing with different types of values, type checking can help you avoid errors. However, this does not mean that you need to explicitly specify the type each time you declare constants and variables.

2> Type speculation

If you do not explicitly specify a type, Swift uses the _ type inference (type inference)_ to select the appropriate type. With the type inference, the compiler can automatically infer the type of the expression when compiling the code . The principle is simple, just check your assigned value.

Because of the type speculation, Swift rarely needs to declare a type than C or objective-c. Constants and variables require explicit typing, but most work does not need to be done by yourself.

Type speculation is useful when you declare constants or variables and assign initial values. When you declare constants or variables, assign them a _ literal (literal value or literal)_ that triggers the type speculation . (The literal is the value that will appear directly in your code, such as ' 42 ' and ' 3.14159 '.) )

① If you assign a value of ' 42 ' to a new constant and do not indicate a type, Swift can infer that the constant type is 'Int', because the initial value you assign to it looks like an integer:

Let Meaningoflife = 42

Meaningoflife will be assumed to be of type Int

② If you do not specify a type for floating-point literals , Swift will speculate that you want 'Double':

Let pi = 3.14159

Pi is assumed to be Double type

   When you speculate on the type of floating-point number, Swift always chooses ' Double ' instead of ' float '.

③ if both integers and floating-point numbers appear in the expression, they are presumed to be of type 'Double':

Let Anotherpi = 3 + 0.14159

Anotherpi will be presumed to be Double type

The original value ' 3 ' does not explicitly declare a type, and a floating-point literal appears in the expression, so the expression is presumed to be a ' Double ' type.

2. Numeric literals1> integer literals can be written:

* A decimal number (decimal) with no prefix

* One binary number (binary), prefixed with '0b'

* An octal number (octal), prefixed with '0o'

* A hexadecimal number (hexadecimal), prefixed with '0x'

Example: The decimal value for all of the following integer literals is ' 17 ':

1  - 2 3 Let Binaryinteger = 0b10001       //  binary45 Let Octalinteger = 0o21
   
    // 
     octal
    6
    7
    0x11     
    // 
     16 binary
   

2> Floating-point number literals

① floating-point literals can be either decimal (without a prefix) or hexadecimal (prefixed with ' 0x '). There must be at least one decimal digit (or hexadecimal number) on either side of the decimal point.

② floating-point literals also have an optional _ exponent (exponent)_, specified in a decimal floating-point number by uppercase or lowercase ' e ' , in the hexadecimal floating-point numbers by uppercase or lowercase ' P ' to specify.

If the exponent of a decimal number is ' exp ', then this number corresponds to the product of cardinality and $10^{exp}$ :

* ' 1.25e2 ' means $1.25x10^{2}$, equals ' 125.0 '.

* ' 1.25e-2 ' means $1.25x10^{-2}$, equals ' 0.0125 '.

If the exponent of a hexadecimal number is ' exp ', then this number is equal to the product of cardinality and $2^{exp}$ :

* ' 0xfp2 ' means $15x2^{2}$, equals ' 60.0 '.

* ' 0xfp-2 ' means $15x2^{-2}$, equals ' 3.75 '.

Example: The following floating-point literals are equal to the decimal ' 12.1875 ':

Let decimaldouble = 12.1875

Let exponentdouble = 1.21875e1

Let hexadecimaldouble = 0xc.3p0

3> Additional formats

Numeric class literals can include additional formatting to enhance readability . Both integers and floating-point numbers can be added with an additional 0 and underlined , without affecting the literal:

Let paddeddouble = 000123.456

Let onemillion = 1_000_000

Let justoveronemillion = 1_000_000.000_000_1

3. Type aliases

The _ type alias (type aliases)_ is to define another name for the existing type . You can use the 'typealias' keyword to define type aliases.

Type aliases are useful when you want to give a more meaningful name to an existing type. Suppose you are working with data for a specific length of external resource:

Typealias audiosample = UInt16

Once you have defined a type alias, you can use the alias anywhere you use the original name:

var maxamplitudefound = Audiosample.min

Maxamplitudefound is now 0.

In this example, ' audiosample ' is defined as an alias of ' UInt16 '. Because it is an alias, ' Audiosample.min ' is actually ' uint16.min ', so the ' Maxamplitudefound ' is assigned an initial value of ' 0 '.

04_SWIFT2 base type safety and type inference + literal + type Alias

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.