Swift language guide (IV)-type security and type inference

Source: Internet
Author: User

Swift is a type security language. The type of security language requires that the type of values in the code is very clear. If your code has a part of the value of the String type, you cannot mistakenly pass the Int type.

In view of Swift's type security, during code compilation, Swift performs a type check and marks any Type mismatch as an error, so that you can capture and correct the error as early as possible during development.

The type check helps you avoid making mistakes when operating on different types of values. But this does not mean that you must check the type when declaring every constant or variable. If you do not check the type of the required value, Swift will execute type inference to calculate the corresponding type.

Type inference allows the compiler to automatically deduce the type of a specific expression based on the value you provide when compiling the code.

Based on type inference, Swift requires much less type declaration than C or Objective-C. Constants and variables still have clear types, but it is up to the compiler to specify the types.

Type determination is particularly useful when you declare a constant or variable and assign an initial value. Usually by assigningNominal value(Literal value, Or"Literal"Literal). (The literal value refers to the value that appears directly in the source code, 42 and 3.14159 in the following example)

For example, if you assign a literal value 42 to a new constant without specifying its type, Swift will infer that you want a constant of the Int type, because the number you provided during initialization is like an integer:

1 let meaningOfLife = 422 // meaningOfLife is inferred to be Int type

Similarly, if the type is not specified for a floating point literal, Swift will infer that you want to create a Double type:

1 let pi = 3.141592 // pi is inferred to be Double type

Swift usually uses Double instead of Float to deduce floating-point numbers ).

If you combine the integer and floating-point literal values in the expression, Swift will deduce the Double type according to the context:

1 let anotherPi = 3 + 0.141592 // anotherPi will be inferred as Double type

In the above example, the literal value 3 does not belong to a specific type or explicitly specifies a type. It is inferred based on the floating point literal quantity of the additional part and the Double type is output as appropriate.

 

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.