Swift Learning Record-Basic Part 2

Source: Internet
Author: User

Integer

Swift provides 8, 16, 32, and 64-Bit Signed and unsigned integer types.

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 uint8 type. Let maxvalue = uint8.max // The value of maxvalue is 255, which is the maximum value of uint8 type.

 

Int

Uint

Note:

Do not use uint unless you really need to store an unsigned integer with the same native character length as the current platform.

The unified use of int can improve code reusability, avoid conversion between different types of numbers, and match the type inference of numbers.

 

Type security and type inference

SWIFT is a type-safe language. A type-safe language allows you to clearly understand the type of the value processed by the Code.

 

Full-value literal

  • One decimal number with no prefix
  • A binary number with a prefix of 0 B
  • An octal number with a prefix of 0 o
  • A hexadecimal number with a prefix of 0x

The floating point literal also has an optional index, which is specified in the decimal floating point number by the upper or lower case e, and in the hexadecimal floating point number by the upper or lower case p.

 

The numeric literal can include additional formats to enhance readability. Both integers and floating-point numbers can be added with extra zeros and underscores, without affecting the literal volume:

let paddedDouble = 000123.456let oneMillion = 1_000_000let justOverOneMillion = 1_000_000.000_000_1

 

 

Integer and floating point conversion

Note:

The combination of numeric constants and variables is different from the combination of numeric literal. The literal volume can be directly added to the word surface volume. They do not have a specific type and can only be inferred when the compiler requires a value.

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.