Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog
in other languages such as C,objective-c , and Java , there are two ways to convert integers:
and in Swift These two methods are not feasible and need to be explicitly converted through some functions, the code is as follows:
Let Historyscore:uint8 = 90
Let englishscore:uint16 = 130
Let totalscore = Historyscore + englishscore// Error
The program will have a compilation error becauseHistoryscoreis aUInt8type, andEnglishscoreis aUInt16types, which cannot be converted between them.
Two methods of conversion:
Code:
Let totalscore = UInt16 (historyscore) + englishscore// is the correct method of conversion.
The other is to UInt16 of the Englishscore converted to UInt8 type. Because it is converted from a large range to a small range number, this conversion is unsafe, and if the number of conversions is larger it can result in loss of precision.
Code:
Let totalscore = Historyscore + UInt8 (englishscore)// is the correct method of conversion.
in this example Englishscore The value is , this conversion is successful, if the number is changed to 1300, although the program compiles no problem, but the exception information is output in the console.
Conversion between integral type and floating point type
Conversions between integral and floating-point types are similar to conversions between integral types:
Let historyscore:float = 90.6
Let englishscore:uint16 = 130
Let totalscore = Historyscore + englishscore// Error
Let totalscore = Historyscore + Float (englishscore)// Right, safe
Let totalscore = UInt16 (historyscore) + englishscore// right, the decimals are cut off
Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php
Swift 2.0 Learning Note (Day 15)--note the conversion between numeric types