Conversions between Swift numeric types

Source: Internet
Author: User
Tags truncated

Swift conversion between numeric types Swift is a safe language, and the checks for types are very strict and cannot be easily converted between different types.
One, the conversion between the integral type
In other languages such as C and objective-c, there are two ways to convert integers:
From the small range number to the large range number conversion is automatic;
Forcing type conversions from a large number to a small range can result in the loss of data precision.
In Swift, these two methods do not work, we need to pass some functions to explicitly convert, the code is as follows:
Let historyscore:uint8 = 90let englishscore:uint16 = 130let Totalscore = historyscore + englishscore  //Error ①let TotalSco Re = 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 totalscore. If you add 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 number. The Code ② Line UInt16 (historyscore) is the correct conversion method.
The other is to convert the UInt16 englishscore to the UInt8 type. Since it is converted from a large range number to a small range number, this conversion is unsafe if the number of conversions compared to the General Assembly results in a loss of precision. Code ③ Line UInt8 (englishscore) is the correct conversion method. Since the value of Englishscore in this example is 130, the conversion is successful, and if the number is changed to 1300, although the program compiles without problems, the exception information is output in the console, which is a run-time exception.
In the code above, UInt16 (Historyscore) and UInt8 (Englishscore) are in fact constructors, capable of creating and initializing another type. The contents of the constructor are described in detail in the 14th chapter.
Conversion between integral type and floating point type
Conversions between integral and floating-point types are similar to conversions between integers, so we'll modify the example in the previous section as follows:
Let historyscore:float = 90.6①let englishscore:uint16 = 130②let Totalscore = historyscore + englishscore
   //error ③let Totalscore = Historyscore + Float (englishscore)  //correct, security ④let Totalscore = UInt16 (historyscore) + Englishscore  //correct, decimal is truncated ⑤


The code above has undergone some modifications, and the ① line code historyscore variable type is the float type. The ② line code englishscore variable or UInt16 type. Where the ③ line code is evaluated directly, the result is a compilation error. The ④ line of code is to convert the Englishscore variable of the UInt16 type to float type, which is the safest conversion. The ⑤ Line code converts the Historyscore variable of type float to the UInt16 type, which first causes the decimal to be truncated, and if the number of Historyscore variables is large, it causes the run-time exception, which is similar to the conversion between integers.


For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485

Welcome to Luxgen iOS Classroom public Platform


Conversions between Swift numeric 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.