Type conversions like Swift

Source: Internet
Author: User

In your code, even if you know that integer constants and variables are non-negative, the int type is used to generalize all integers. Using the default integer type in normal times means that integer constants and variables can be manipulated in a timely manner, and can be matched by an integer literal value to match the inferred type.

Use other integer types only when there are some specific requirements for the current task, because the size of the data coming from outside is clear, or because of performance, memory usage, or other necessary optimizations. In these cases, the size of the explicit type is used to help catch unexpected values overflow and is used for implicit text in the original value.

Integer conversions


The range of numbers that can be stored for constants or variables of each numeric type is different. A Int8 constant or variable can store a number from 128 to 127, and then a constant or variable of int can store a number from 0 to 255. When a number is assigned outside the range expressed by a constant or variable of an integer type, an error is reported when compiling the code:

Let cannotbenegative:uint8 =-1//UInt8 cannot store a negative number

Let toobig:int8 = Int.max + 1//Int8 cannot store a value larger than its maximum value

Because the first number type stores different ranges of values, you must choose to add a numeric type conversion on an individual basis. This selection mechanism prevents hidden conversion errors to help you make explicit type conversion intents.

To convert a specified number type to another type, you can use this number to initialize a new number of the type you want to expect. In the example below, the constant Twothousand type is UInt16, however, the type of the constant one is UInt8, and they cannot be added directly together because their type is different. In this example, you can call UInt16 (one) as the initial value to create a new UInt16 that replaces the original position with this new number.

Let?? Twothousand?:? UInt16? =? 2_000

  Let?? one?:? UInt8? =? 1

? Let?? Twothousandandone? =? Twothousand? + ? UInt16? (? one?)

This addition is allowed because the types on both sides of the addition are UInt16. The type of the output constant (Twothousandandone) is inferred to be UInt16, because it is the and of two UInt16 values.

The Swift type initializes the default method called SomeType (initialization value), such as double (value), Int8 (value), and passes the initial value to it. In the background, UINT16 has an initialization program to receive a UInt8 value, which creates a new UInt16 by the value of the UInt8 received. There is no way to pass a type here, however, it must provide a UInt16 type initialization program. Extending an existing type provides an extension that receives a new type of initialization program (containing its own type definition) to overwrite.

Conversion of integers and floating-point numbers

Conversions between integers and floating-point numbers must show the indicated:

Let three = 3

Let Pointonefouronefivenine = 0.14159

Let pi = double (three) + pointonefouronefive//pi = 3.14159 pi is of type double

Here, the value of the constant three is used to create a new number of type double, so the type on either side of the plus sign is the same. If no conversion is made, this addition operation will not be allowed to pass.

Floating-point-to-integer must also be clearly specified. An integer can be initialized by either a single-digit degree or by a singular number:

Let Integerpi = Int (PI); INTEGERPI = 3 Type int

When a floating-point number is converted to an integer, the value of its floating-point numbers is most likely to be truncated in fractional parts. This means that 4.75 is converted into a 4,-3.9 converted to-3.

Note: The combined standard and numeric literal values for numeric constants and variables are different. The literal 3 can be directly added to the literal 0.14159, because the numeric literal does not have a definite type, and their type is only indicated by compiler inference.

Type conversions like Swift

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.