Swift Basic Types

Source: Internet
Author: User

First, the number type:

Conversion between Swift numeric types Swift is a safe language, and for type checking is very strict and cannot be easily converted between different types.

1. Conversion between integers: In other languages such as C and objective-c, there are two methods for integral types

From the small range number to the large range number conversion is automatic;

Forcing 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 for the display to convert, the code is as follows:

Let start:uint8=10;

Let end:uint16=20;

Let Total=start+end//This is wrong

Let Total=uint16 (Start) +end//Correct procedure 1

Let Total=start+uint8 (end)//correct procedure 2

Second, the Boolean data type:

The Boolean variable 2 is stored as a numeric form of 8 bits (one byte), but can only be true or false.

1. When called as a constructor (with operator new), Boolean () converts its arguments to a Boolean value and returns a Boolean containing the value.

2. If called as a function (without operator new), Boolean () will only convert its arguments to an original Boolean value and return this value.

The code is as follows:

Let Aa=true;

Let Bb=false;

Three, strings and characters

String is an ordered set of characters, such as "Hello,world". Swift strings are represented by a string type and can also be represented as a collection of character type values. Swift's string and character types provide a fast, Unicode-compatible way to handle textual information in code. The syntax for creating and manipulating strings is similar to the way C works, lightweight and readable. The string join operation only needs to pass the + number two strings next year. As with other values in swift, you can change the value of a string, depending on what is defined as a constant and variable.

The code is as follows:

Let's somestring= "Hello World";

Swift Basic 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.