Swift Programming Language Learning-annotations, semicolons, integers, floating-point numbers

Source: Internet
Author: User

Swift Programming Language Learning-annotations, semicolons, integers, floating-point numbers

Note

Note the non-execution text in your code into prompts or notes for future reading. The Swift compiler automatically ignores the comments when compiling the code.

Notes in Swift are very similar to those in C. A single line comment starts with a double Forward slash:

// This is a comment

You can also perform multi-line comments, starting with a single forward slash followed by an asterisk (/*), ending with an asterisk and following a single forward slash (*/):

/* This is a multi-line comment */

Unlike multi-line comments in C, Swift multi-line comments can be nested in other multi-line comments. You can create a multi-line comment block, and then nest it into a second multi-line comment block. When terminating a comment, insert the ending mark of the second comment block, and then insert the ending mark of the first comment block:

<Pre name = "code" class = "java">/* this is the beginning of the first multi-line comment/* this is the second nested multi-line comment */This is the first multi-line comment end of line comment */

 

By using nested multi-line comments, you can quickly and easily comment out a large piece of code, even if the Code already contains multi-line comments.

Semicolon

Unlike most other programming languages, Swift does not require you to use semicolons (;) at the end of each statement. Of course, you can add semicolons as you like. In one case, you must use a semicolon to write multiple independent statements in the same row:

let cat = "

Integer

An integer is a number without decimal digits, such as 42 and-23. Integers can be signed (positive, negative, zero) or unsigned (positive, zero ).

Swift provides 8, 16, 32, and 64-Bit Signed and unsigned integer types. These integer types are similar to those in the C language. For example, the 8-bit unsigned integer type is UInt8, and the 32-bit signed integer type is Int32. Like other types of Swift, the integer type adopts the upper-case naming method.

Integer Range

You can access the min and max attributes of different integer types to obtain the maximum and minimum values of the corresponding types:

Let minValue = UInt8.min // The value of minValue is 0, which is the minimum value of the UInt8 type.

Let maxValue = UInt8.max // maxValue is 255, which is the maximum value of the UInt8 type.

Int

Generally, you do not need to specify the length of an integer. Swift provides a special integer Int with the same length as the native character of the current platform:

On a 32-bit platform, Int and Int32 are of the same length.

On a 64-bit platform, Int and Int64 are of the same length.

Unless you need an integer of a specific length, Int Is generally enough. This improves code consistency and reusability. Even on 32-bit platforms, Int can store an integer range of-2147483648 ~ 2147483647, most of the time this is big enough.

UInt

Swift also provides a special unsigned UInt with the same length as the native character of the current platform:

On a 32-bit platform, UInt and UInt32 are of the same length.

On a 64-bit platform, the length of UInt is the same as that of UInt64.

Note:

Do not use UInt unless you really need to store an unsigned integer with the same native character length as the current platform. In addition to this situation, it is best to use Int, even if the value you want to store is known to be non-negative. The unified use of Int can improve code reusability, avoid conversion between different types of numbers, and match the type speculation of numbers. Please refer to type security and type speculation.

Floating Point Number

A floating point number is a number with a decimal part, such as 3.14159, 0.1, and-273.15.

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 indicates a 64-bit floating point number. Use this type when you need to store large or high-precision floating point numbers.

Float indicates a 32-bit floating point number. This type can be used if the precision requirement is not high.

Note:

Double has a high precision, with at least 15 digits while Float has at least 6 digits. The type you select depends on the range of values to be processed by your code.

 

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.