Swift's built-in digital types and basic conversion methods _swift

Source: Internet
Author: User

Although you write any programming language, you need to use different variables to store all kinds of information. Variables are simply reserved memory locations to store values. This means that when you create a variable, some space is preserved in memory.

Might like to store information like strings, characters, wide characters, integers, floating-point numbers, Boolean, and various data types. Depending on the data type of a variable, the operating system allocates memory and determines what can be stored in memory.

Built-in data types
Swift provides programmers with built-in and user-defined kinds of data types. The following is a list of basic data types that declare variables to be used most frequently:

Int or UInt-this is used for integers. More specifically, you can use Int32,int64 to define a 32-or 64-bit signed integer that is used as a variable for UInt32 or UInt64 to define a 32 or 64-bit unsigned integer. For example, and-23.

    • Float-this is used to represent a 32-bit floating-point number, which is typically used to use smaller decimal points. For example: 3.14159, 0.1, and-273.158.
    • Double-This is used to represent a 64-bit floating-point number for very large floating-point values. For example: 3.14159, 0.1, and-273.158.
    • Bool-This represents a Boolean value, True or false.
    • String-This is an ordered character set combination. For example, "Hello, world!"
    • Character-This is a single character string. For example, "C"
    • Optional-This represents a variable that can hold a value or have no value.

You have the following points about integer types:

    • On a 32-bit platform, the Int size is the same size as the Int32.
    • On a 64-bit platform, the Int size is the same size as the Int64.
    • On a 32-bit platform, the UInt size is the same size as the UInt32.
    • On a 64-bit platform, the UInt size is the same size as the UInt64.
    • Int8, Int16, Int32, Int64 can be used to represent 8 bit, bit, bit and bit to form a signed integer.

UInt8, UInt16, UInt32 and UInt64 can be used to represent 8 bit, bit, bit, and bit to form unsigned integers.
bound value
The following table shows the variable type, how much memory is required to store this value in memory, where variables of this type can store both maximum and minimum values.

Type Alias
you can use Typealias to create a new name from an existing type. The following is a simple Typealias syntax to define a new type:

Copy Code code as follows:

Typealias newname = Type

For example, the following tells the compiler that feet is another name for Int:
Copy Code code as follows:

Typealias Feet = Int

Now, the following declaration is completely legal and creates an integer variable of distance:
Copy Code code as follows:

Import Cocoa

Typealias Feet = Int
var distance:feet = 100
println (distance)


When we use playground to run the above program, to the following results.

100

Type safety
Swift is a secure language, which means that if part of your code requires a string, you cannot pass an int incorrectly.

Because Swift is type-safe, it executes the compile code and flags a type mismatch error when the type is checked.

Copy Code code as follows:

Import Cocoa

var VarA = 42
VarA = "This is Hello"
println (VarA)


When we compile the above program it produces the following compile-time error.
Copy Code code as follows:

Playground execution Failed:error:: 6:6: Error:cannot assign to ' let ' value ' VarA '
VarA = "This is Hello"

type Inference
When it compiles code, type inference causes the compiler to automatically infer the type of the particular expression, simply by checking the supplied value. Swift uses type inference to work out the appropriate types as follows.
Copy Code code as follows:

Import Cocoa

VarA is inferred to being type Int
var VarA = 42
println (VarA)

Varb is inferred to being type Double
var Varb = 3.14159
println (Varb)

Varc is also inferred of type Double
var Varc = 3 + 0.14159
println (VARC)


When we use playground to run the above program, we get the following results.


3.14159 3.14159


The conversion between swift numeric types
conversion between Swift numeric types Swift is a safe language, very strict with types, and cannot be easily converted between different types.
conversion between integral types
in other languages, such as C and objective-c, there are two ways to convert an integral type:
The conversion from a small range to a large range is automatic;
The need to force type conversions from a large range to a small number of numbers may result in loss of data precision.
In Swift, these two methods are not workable, and we need to explicitly convert them through some functions, as follows:

Copy Code code as follows:

Let Historyscore:uint8 = 90


Let englishscore:uint16 = 130


Let Totalscore = Historyscore + englishscore//Error ①


Let Totalscore = UInt16 (historyscore) + englishscore//correct ②


Let Totalscore = Historyscore + UInt8 (englishscore)//correct ③

The above code declares and initializes two constants Historyscore and Englishscore, and we add them to the Totalscore. If you add with the ① line code, the program will have a compilation error because Historyscore is a UInt8 type and Englishscore is a UInt16 type and cannot be converted between them.
We have two methods of conversion.
One is to convert the UInt8 historyscore to the UInt16 type. This conversion is safe because it is converted from a small range to a large range. The Code ② Line UInt16 (historyscore) is the correct conversion method.
The other is to convert UInt16 englishscore to UInt8 type. Since it is converted from a large number of numbers to a small range, this conversion is unsafe if the number of conversions is compared to the loss of the precision caused by the assembly. The Code ③ Line UInt8 (englishscore) is the correct conversion method. Because the value of englishscore in this example is 130, this conversion is successful, if you modify this number to 1300, although the program compiles no problem, but it will output exception information in the console, this is a run-time exception.
In the preceding code, UInt16 (Historyscore) and UInt8 (Englishscore) are in fact constructors, capable of creating and initializing another type. conversion between integral type and floating point type
the transformation between an integral type and a floating-point type is similar to that of an integral type, so we modify the example in the previous section as follows:

Copy Code code as follows:

Let historyscore:float = 90.6①


Let englishscore:uint16 = 130②


Let Totalscore = Historyscore + englishscore//Error ③


Let Totalscore = Historyscore + Float (englishscore)//correct, safe ④


Let Totalscore = UInt16 (historyscore) + englishscore//correct, decimal is truncated ⑤


The above code has undergone some modification, ① line code historyscore variable type is float type. The ② line code englishscore variable or UInt16 type. In which the ③ line code is calculated directly, resulting in a compilation error. The ④ line of code is to convert the Englishscore variable of the UInt16 type to a float type, which is the safest conversion. The ⑤ line of code is to convert a float-type historyscore variable to a UInt16 type, which causes the decimal to be truncated first, and, if a large number of historyscore variables, causes a Run-time exception, which is similar to the conversion of the integral type.


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.