Breeze annotation-Swift programming language: Point6 ~ 10,-swiftpoint6

Source: Internet
Author: User

Breeze annotation-Swift programming language: Point6 ~ 10,-swiftpoint6

Directory Index

Breeze annotation-Swift Programming Language

Point 6.

Output constants and variables

Code example:

// The output content will wrap println ("hello, world") // The output content will not wrap print ("My name is Dash Geng") at the end ")

Note:

  • You can use the println or print function to output the value of the current constant or variable.
  • Println and print are global functions used for output (built-in functions ).
  • The output content of the println function is wrapped in a line break.
  • The output content of the print function does not last wrap.
  • Swift uses string interpolation to add a constant or variable name to a long string as a placeholder. During code execution, these Placeholders are replaced with the values of the current constant or variable.

 

Point 7.

Swift code comment

Code example:

/* Swift comments include multiline comments (/**/) and single-line comments (//) /* multi-line comments can be nested with multi-line comments * // single-line comments can also be nested with multi-line comments */

Note:

  • Like other languages, the Swift compiler automatically ignores comments when compiling code.
  • Swift has two annotation styles: single-line comment and multi-line comment.
  • Single line comment with Double Forward slash (//.
  • The start of a multi-line comment is marked as a single forward slash followed by an asterisk (/*), Ending with an asterisk followed by a single forward slash (*/).
  • Swift multi-line comments can contain single-line comments or nested multi-line comments.

 

Point 8.

Integer type

Code example:

// Signed integer var temperature: Int8 =-8var height: Int16 = 179var monthlyExpenditure: Int32 =-4680var landArea: Int64 = 9_600_000 // unsigned integer var age: UInt8 = 32var years: UInt16 = 2014var moonDistance: UInt32 = 384400var globalPopulation: UInt64 = 7_000_000_000

Note:

  • An integer is a number without decimal digits.
  • Swift provides 8, 16, 32, and 64-Bit Signed and unsigned integer types.
  • Signed integer type: Int, Int8, Int16, Int32, Int64.
  • Unsigned integer type: UInt, UInt8, UInt16, UInt32, UInt64.
  • All data types of Swift use the upper-case naming method.
  • You can access the min and max attributes of different integer types to obtain the maximum and minimum values of the corresponding type.
  • // Minimum value of the 16-bit signed number:-32768let minInt16Num: Int16 = Int16.min // minimum value of the 16-bit unsigned number: 0let minUInt16Num: UInt16 = UInt16.min // maximum value of a 32-bit signed integer: 2_147_483_647let maxInt32Num: Int32 = Int32.max // maximum value of a 32-bit unsigned integer: 4_294_967_295let maxUInt32Num: UInt32 = UInt32.max
  • Swift provides a special integer Int with the same length as the native character of the current platform. On 32-bit platforms, Int and Int32 are of the same length. On 64-bit platforms, Int and Int64 are of the same length.
  • /* The length of the Int type depends on the current CPU font length, and the local font length is 64 bits. Therefore, maxIntNum is 9_223_372_036_854_775_807 */let maxIntNum: Int = Int. max
  • Swift also provides a special unsigned UInt with the same length as the native character of the current platform. On a 32-bit platform, the length of UInt is the same as that of UInt32. On a 64-bit platform, the length of UInt is the same as that of UInt64.
  • /* The length of the UInt type depends on the current CPU length, and the local font length is 64 bits. Therefore, maxUIntNum is: 18_446_744_073_709_550000615 */let maxUIntNum: UInt = UInt. max
  • Do not use UInt, but use Int in a unified manner. This improves code reusability, avoids conversion between different types of numbers, and matches the type inference of numbers.

 

Point 9.

Floating Point Type

Code example:

Var floatNum: Float = 1.12345678901234567890var float32Num: Float32 = 1.12345678901234567890var float64Num: Float64 = 1.12345678901234567890var float80Num: Float80 = 1.12345678901234567890var doubleNum: Double = 1.12345678901234567890 // The value of floatNum is: 1.123 _ percentile ("\ (floatNum)") // The value of float32Num is 1.123 _ 456_835_746_77println ("\ (float32Num)") // The value of float64Num is: 1.123 _ percentile ("\ (float64Num)") // The value of float80Num is 1.123 _ 456_789_012_35println ("\ (float80Num)") // The value of doubleNum is: 1.123 _ 456_789_012_35println ("\ (doubleNum )")

Note:

  • A floating point number is a number with a decimal part.
  • The floating point type has a larger range than the integer type. It can store numbers that are larger or smaller than the Int type.
  • Swift provides two types of signed floating point numbers: Double and Float.
  • Float indicates a 32-bit floating point number, and Double indicates a 64-bit floating point number.
  • Float is accurate to 6 digits, and Double is accurate to at least 15 digits.
  • In fact, Swift has five optional types of signed floating point numbers: Float, Float32, Float64, Float80, and Double.
  • On a 64-bit processor, Float and Float32 have the same value range. Float64, Float80, and Double have the same value range.

 

Point 10.

Type security and type inference

Code example:

// The assignment of constants or variables triggers type inference. // Type inference the positive integer is inferred as Int type var index = 1 // type inference the floating point is inferred as Double type var pai = 3.14

Note:

  • Swift is a type-safe language. The Type-safe language allows you to clearly understand the type of the value to be processed by the Code.
  • Because Swift is type-safe, it performs type checks during code compilation and marks mismatched types as errors.
  • If the type is not explicitly specified, Swift uses type inference to select the appropriate type.
  • When declaring constants or variables, assign them a literal to trigger type inference.
  • Type inference infers Positive Integers to the Int type and floating point numbers to the Double type.

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.