In Swift Language Tutorial for Beginner 1 - Variable and Constant, we learned about Swift’s built-in data types, and this time we’ll take a look at the basic data types in Swift.
Built-in type summary
First, let’s take a look at Swift’s built-in data types through a mind map
As you can see, the built-in types in Swift are basically the same as in other high-level languages. Relative to C/C++, Java, the less common are tuple types and optional types (also available after Java 8)。 Each type name begins with an uppercase letter。
Next, we’ll show the main points of each type (except tuples and optionals, which will be discussed separately later).
Integer
There are two categories, conforming and unsigned, and each class can be divided into thinner categories according to the length used for storage, the list is as follows:
Unsigned Integer
1 |
|
Signed integer
1 | var maxInt8 = Int8.max //127 |
We can also use Int or UInt to directly define integer data, in which case different Ints and UInts of the machine will be mapped to different types, such as in 64-bit machines, Int maps to Int64, UInt maps to UInt64.
Floating-point type
Similar to other high-level languages, floating-point types are divided into single precision and double precision. as follows:
1 | var b = MemoryLayout<Float>.size //4 bytes |
When defining, scientific computation can be used
1 | var sum = 1.25e3 //1.25*(10^3) = 1250 |
You can also use the symbol “_” to split
1 | var num1 = 001.23 |
Boolean
There are only two explicit values
1 | var bool1 = true |
String
As in other languages, strings enclosed in double quotation marks are used. Stitching can be done directly by the “+” sign.
1 | var str = "hello,playground" |
Next
Next, we’ll briefly introduce the basic control statements in Swift Swift Language Tutorial for Beginner 3 - Control statements.