Swift Basic Learning (II.) data types

Source: Internet
Author: User
Tags value of pi

In programming languages, different data types are required to be stored differently, and the amount of memory allocated by the computer in memory for different data types is different, and specific values require specific types to declare.

Swift Data types

Built-in data types

Integers-Int & UInt

According to the memory space occupied by an integer variable (8/16/32/64-bit, which is now basically all 64-bit for IOS development) and whether integers are signed (unsinged), Swift defines 8 different integer types altogether:

  Int8/int16/int32/int64

  Uint8/uint16/uint32/uint64

  The first line represents a signed integer, and the second line represents an unsigned integer.

Typically, these specific integer types are not used directly in your code, only Int is used to define signed integers, and a UInt is used to define unsigned integers. The Swift compiler converts an Int or UInt to the corresponding integer type based on the current target compilation platform.

For example, on a 32-bit platform, int and Int32 represent integers of equal length.

On a 64-bit platform, use the mix and Max methods, respectively, to see how the values expressed by Int and Int64 are:

1Print"int.min = \ (int.min)")2Print"Int.max = \ (Int.max)")3Print"int64.min = \ (int64.min)")4Print"Int64.max = \ (Int64.max)")5 6 Printing results:7Int.min =-92233720368547758088Int.max =92233720368547758079Int64.min =-9223372036854775808TenInt64.max =9223372036854775807

The range of Int and Int64 expressions is the same.

At the same time in Swift, you can use a different binary to express an integer:

1  the // 10 binary 2 0xF // 16 binary 3 // 8 binary 4 // 2 binary

And using underscores in numbers:

1 let million = 1_000_000

Tip: Unless your hardware platform specifically requires that you use UInt to define unsigned integers, you should use Int as much as possible to express all the integer types, even if you can determine that an integer must be non-negative, which reduces the hassle of many unnecessary type conversions.

Floating-point number-float & Double

In Swift, there are two different types of floating-point numbers depending on the accuracy of the expression:

float: A floating-point number that expresses up to 6 bits of precision.

Double: A floating-point number that can represent at least 15 bits of precision.

1var onethirdinfloat:float =1/32var onethirdindouble:double =1/33 4Print"\ (onethirdinfloat)")5Print"\ (onethirdindouble)")6 7 Print:8 0.3333339 0.333333333333333

You can also use scientific notation to represent floating-point numbers:

1 0.314e1 2 PI = 314e-2

Tip: If there is no explicit requirement, you should use Double to define the floating-point number uniformly.

The Type inference associated with the number

In Swift, when a variable does not specify a type, the compiler pushes the type of the integer down to Int, which pushes the type of the floating-point number to Double:

1var three =32Print"the type of 3 is: \ (type (of:three))")3 4var zeropointforteen =0.45Print"the type of 0.4 is: \ (type (of:zeropointforteen))")6 7 Print:8  3the type is: Int9  0.4The type is: Double

In Swift, you can calculate the literal value of different types of numbers directly:

1 3 0.14 2 print ("  value of pi: \ (pi), type of pi: \ (Type (OF:PI))")34 Print: 5  3.14, the type of PI is: Double

But it is not possible to do arithmetic operations on variables of different numeric types, and when you do arithmetic on variables, the types of all variables must be the same, and if the variable types are different, you must explicitly convert some of the variables to type:

1 var sum = Double (three) + Zeropointforteen

As can be seen from the above, the use of a Double (three) of an Int type of three "conversion" into a floating-point number, here, the reason for the conversion is quoted, because it does not really convert the three type from Int to Double, but with the value of three, the initialization of a new A double with a value of 3 and adds the new double and Zeropointforteen.

Reference Link: HTTPS://WWW.BOXUEIO.COM/SERIES/SWIFT-UP-AND-RUNNING/EBOOK/2

Swift Basic Learning (II.) data types

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.