Swift-basic data type (1)

Source: Internet
Author: User
Tags ranges

Unlike in more languages, X does not require you to write a semicolon (;) after every statement in your code, although this can be done. However, it is necessary to write multiple separate statement semicolons in a row:

. 1 letcat = ""; println (CAT)

. 2 // prints ""

Data Type

Integers

Integers are integers without decimals, such as 42 and-23. An integer is signed (positive, zero or negative) or unsigned (positive or zero ).

Swift provides 8, 16, 32, and 64-bit symbols and unsigned integers. These integers follow the naming convention similar to C. In an 8-bit unsigned integer uint8, and a 32-bit signed integer is of the int32 type.

Integer Bounds

The minimum and maximum values of each integer type can be asked with the minimum and maximum values of its maximum and minimum attributes.

1 letminvalue = uint8.min // minvalue is equal to 0, and is of Type uint8

2 letmaxvalue = uint8.max // maxvalue is equal to 255, and is of Type uint8

The values of these attributes are numbers of the appropriate size (for example, uint8 in the preceding example), and
It can be used because it can be used along expressions of other values of the same type.

Int

In most cases, you do not need to select the specific size of the integer to use in the code. Swift provides an extra Integer type, Int, which has the same size as the local word size of the current platform:
? On a 32-bit platform, int32 of the same size is interpreted.
? On a 64-bit platform, int64 of the same size is interpreted.
Unless you need to work with an integer of a specific size, the integer value in the int code is always used. This facilitates code consistency and interoperability. Even on 32-bit platforms, interpreting can store no matter what value-2,147,483,648 to 2,147,483,647, and there are many integer ranges that are large enough.

Uint

Swift also provides an unsigned integer type, uint, which has the same size as the local word size of the current platform:
? On a 32-bit platform, uint is a uint32 of the same size.
? On a 64-bit platform, uint is the same size as uint64.

 

Floating-point numbers

Float and decimal digits, such as 3.14159, 0.1, and-273.15. The floating point type can represent values in a wider range than the integer type, and can store numbers,
Is larger or smaller than can be stored in an int. Swift provides two types of signed floating point numbers:
? Double represents a 64-bit floating point number. When using it, the floating point value must be large, or
Especially accurate.
? A floating point represents a 32-bit floating point number. When using it, the floating point value does not require 64-bit precision.

Type Safety and type inference

SWIFT is a type of secure language. The type security language inspires you to understand the type code of the value to work. Assume that the part of your code requires a string, and you cannot pass an interpretation by mistake.
Because SWIFT is type-safe, it will run the type check when compiling your code and flag no matter what type does not match. This allows you to capture and correct errors during development as early as possible.
The type check helps you avoid errors when you are using different types of values. However, this does not mean that you must specify the type declared by each constant and variable. If no value is specified, swift uses type inference to specify the type you need. When the compiler determines the type of a specific expression, it compiles your code and only needs to check the value you provide.
Because of the type determination, swift requires a much fewer type declaration than a language, such as C or objective-C. Constants and variables are still explicitly typed, but most of the work to specify their types is done for you.
It is particularly useful when you declare the initial value type of a constant or variable. This is generally done by assigning a text value (or text) to the declared point of a constant or variable. (Literally, the value is directly from the current source code, such as the following sample 42 and 3.14159. )

For example, suppose you specify 42 to a new constant, not to mention its type. Swift judges that the constant you want is an interpretation, since you have initialized it with something that looks like an integer

. 1 letmeaningoflife = 42

. 2 // meaningoflife is inferred to be of typeint

Same. If you do not specify the float value of the type, swift determines that you want to create a double

:

. 1 letpi = 3.14159

. 2 // pi is inferred to be of Type Double

When swift selects double instead of float, it determines the floating point type.
Assume that you set the integer and floating point constant in an expression. A double type is determined from the context:

. 1 letanotherpi = 2 + 0.14159

. 2 // anotherpi is also inferred to be of typedouble

 

Numeric literals

Integer constants can be written as follows:
? One decimal number without a prefix
? A binary number with the prefix 0b
? An octal number, prefixed with 0 °
? A hexadecimal number prefixed with 0x

All these Integer constants have 17 decimal values:

. 1 letdecimalinteger = 17

2 letbinaryinteger = 0b10001 // 17 in binary notation

.

3 letoctalinteger = 0o21 // 17 in octal notation

.

4 lethexadecimalinteger = 0x11 // 17 inhexadecimal notation

.

Floating Point text can be in decimal (without prefix) or hexadecimal (prefix with 0x ). They must always have a two-digit number (or a hexadecimal number) at the decimal point ). They can also have an optional index, represented by an upper or lower case e representing a decimal floating point number, or an upper or lower case P representing a hexadecimal floating point number.

Returns the exp index of the decimal number. The base number is multiplied by 10exp:

? 1.25e2means1.25 × 102, or125.0.

? 1.25e-2means1. 25 × 10-2, or0.0125.

Returns the exponent of hexadecimal number and Exp. The base number is multiplied by 2exp:

? 0xfp2means15 × 22, or60.0 .?? 0xfp-2means15 × 2-2, or3.75.

All these floating point constants have 12.1875 decimal values:

. 1 letdecimaldouble = 12.1875

. 2 letexponentdouble = 1.21875e1

. 3 lethexadecimaldouble = 0xc. 3P0

Digital text can contain additional formats to make it easier to read. The two integers and floating-point numbers can be filled.
There is an extra zero and can contain underscores to help readability. Regardless of the format, the basic value of the text is affected:

. 1 letpaddeddouble = 000123.456

. 2 letonemillion = 0000000_000

. 3 letjustoveronemillion = 0000000_000.000_000_1

 

Numeric type conversion

Use all common Integer constants and variables in int type code, even if they are known non-negative. In daily life, the default Integer type means that Integer constants and variables are directly interoperable in the code, and the matching type is determined as an integer text value.
When used only, it is especially necessary for their tasks at hand, other integer types, because the data size is understood from external sources, or performance, memory usage, or other necessary optimizations. In these cases, explicit data types can be used to capture the nature of any unexpected overflow value or the data used by implicit records.

Integer Conversion

It can be stored in the range of an integer constant or variable number, which is different from each numerical type. An int8 constant or variable can store numbers between-128 and 127, while a uint8 constant or variable can store numbers between 0 and 255. A number will not be placed into a constant or variable of the integer type of the size reported as an error when your code is compiled:

. 1 letcannotbenegative: uint8 =-1

. 2 // uint8 cannot store negative numbers, andso this will report an error

. 3 lettoobig: int8 = int8.max + 1

. 4 // int8 cannot store a number larger thanits maximum value,

. 5 // and so this will also report an error

Since each numeric type can store different value ranges, you must choose to add? Converts numeric values based on case-by-case. This method prevents hidden conversion errors and helps you understand the type conversion intent in your code.
To convert a specific numeric type to another one, you initialize a new number of the type required by the existing value. In the following example, constant twothousand is of Type uint16, while constant 1 is of Type uint8. They cannot be added together directly? Because they are of the same type. Instead, the question ?? The demo calls (one) of uint16 to create a new uint16 variable whose value is initialized, and uses this value to replace the original one:

. 1 lettwothousand: uint16 = 2_000

. 2 letone: uint8 = 1

. 3 lettwothousandandone = twothousand + uint16 (one)

Because of adding? For now, except for consent. The output constant (twothousandandone) is determined to be of Type uint16, because it is the sum of the values of two uint16.
Sometype (ofinitialvalue) is the default method to call the swift class initialization and pass an initial value. Behind the scenes, uint16 has an initial value set to accept the uint8 value, so this initialization is used to create a new uint16 from the existing uint8. You cannot pass whatever type here, but it must be a type of uint16 that provides an initialization. Extend existing types, specifying acceptance of new types (including your own Type Definitions) is covered in extension initialization.

Integer and floating-point conversion

. 1 letthree = 3

. 2 letpointonefouronefivenine = 0.14159

. 3 letpi = double (three) + pointonefouronefivenine

. 4 // pi equals 3.14159, and is inferred to beof type double

Here, the value of constant 3 is used to create a new value of the double type, so that except the two values are of the same type. If this conversion is not in place, it will not be agreed.
In turn, the conversion from a floating point to an integer can be initialized with a double or float value in an integer type:

. 1 letintegerpi = int (PI)

. 2 // integerpi equals 3, and is inferred tobe of Type int

When the floating point value used to initialize a new integer in this way is always truncated. This means that 4.75 is changed to 4, and-3.9 is changed to-3.

 

Type aliases

The Type alias is an alternative name defined by the existing type. You can define the type alias and typealiaskeyword. When the name you want to use is context, it is more suitable to indicate that the existing type alias is practical,
For example

Typealias audiosample = uint16

Once you define a type alias, you may use the original name wherever you can:

. 1 varmaxamplitudefound = audiosample. Min

. 2 // maxamplitudefound is now 0

Here, audiosample is defined as an alias uint16. Because it is an alias, calling audiosample. Min actually calls uint16.min, which provides an initial value of 0 for the maxamplitudefound variable.

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.