Definition of Swift type conversions and type aliases (Typealias)

Source: Internet
Author: User
Tags aliases

(i) Type conversion

type conversions are more stringent in Swift, and can be considered as not mutually transforming between different types, and can only regenerate one object and value and copy one.

1.0 The conversion between integer values.

// different types cannot be added directly, then we need to use the type conversion  A   +  Int16 (int8) print (int16)// output:

Again, it is possible to get the correct value through conversion, but it is worth noting that the conversion from a short to a long integer can be successful without losing value, and if a long integer is converted to a short integer, it may be unsuccessful.

Such as:

1234//  error, cannot be converted because 1234 has exceeded the maximum value that the Int8 type can represent (127)

So Swift also suggests that we try to use Intinstead of explicitly using Int8, UInt,and so on.

2.0 conversion between integer and floating-point values

Let A:int  //  output 10.010.2//  output 10, Therefore, the floating-point class to the integral type conversion, will lose the decimal point after the part, will lose the precision

so we can conclude that the accuracy of the high precision to the low conversion, will be lost, and vice versa.

(ii) type aliases

The so-called type alias: refers to a type to take an alias. Use the keyword typealias here .

Typealias myInt =//

Definition of Swift type conversions and type aliases (Typealias)

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.