Data Types in Swift
In Swift 4 programming language, data types are used while creating a variable or a constant. In every programming language, we have to use different types of variables to store data. Variables are reserved memory locations for storing values. This means when you a variable, you reserve some space in memory.
A data type is a type of data (value) a variable can store in it. This data can be a text/string (“tutoraspire”) or a number (12.34) or just bits (0 & 1). We have to define the data type to ensure that only defined type of data is stored.
For example,
Suppose you have to store data of a student where you need to store his name and marks. The mark is a number (i.e. 70) and the name is a string (i.e. Alex) so, you need to create two variables or constants to store the data.
In Swift 4, you can do this by declaring variables and the data type as:
Here, we declared Marks variable of type Int which stores value 70 and, Name variable of type String which stores value Alex.
You will get a compile time error if you use the following syntax:
It will show a message stating cannot convert value of type ‘String’ to specify type ‘Int’.
It shows that a number can only be stored in Int data type not in String data type.
Size of a Data Type
Size of the data type specifies the size of data that can be stored in a given variable or constant. The data type’s size is measured in bit and it can store a value up to 2bits.
So, data type size 1 bit can store upto 21 =2 values: either 0 or 1.
Data type size 2 bit can store up to 22 = 4 values. Which are: (00,01,10,11). It can be represented as:
Same as a data type of n bit system can store up to 2n values.
Swift 4 Data types
The most common Swift 4 data types are:
Bool Data type
- The Bool data type can store only two values either true or false.
- Its default value is False.
- It is generally used in if-else statement.
Boolean Data type example:
Output:
true
Integer Data type
- The integer data type can store both positive and negative numbers including zero with no fractional values.
- Its default value is 0.
- Its size depends on the platform type which may be 32 bits or 64 bits.
- Range: -2,147,483,648 to 2,147,483,647 (32 bit platform)
-9223372036854775808 to 9223372036854775807 (64 bit platform - Integer data type has many other variants such as UInt, Int8, Int16 etc. which can be used if you want to specify storage type specifically.
Integer Data type example:
Output:
50 40
Explanation: In the above program, we have declared a variable highScore of Int type and then assign a value of 50 so; it provides an output of 50.
Later, we changed the value to 40 using assignment operator highScore = 40 so, it provides the output 40.
There are some more variants of Int data type in Swift.
Int8 Data type:
Int8 data type can store both positive and negative small numbers. Its default value is 0, size is 8 bit and range varies from -128 to 127.
It can store 28 values means values from -128 to 127 including 0.
You can check the highest and lowest values Int8 data type can store by using .min and .max.
Example of .min and .max Int8 data type
Output:
-128 127
UInt Data type
UInt (Unsigned Integer) can only store unsigned numbers (positive and zero). Other remaining properties are same as Integer data type means:
- Default Value: 0
- Size: 32/64 bit depends on the platform type.
- Range: 0 to 4294967295 (32 bit platform)
0 to 18446744073709551615 (64 bit platform)
Float Data type
The float data type can store decimal and fractional numbers. Its default value is 0.0, size 32 bit and range varies from 1.2*10-38 to 3.4 * 1038 (~6 digits).
Float data type example:
Output:
3.1416
Double Data type
The Double data type can store decimal or fractional numbers as Float data type but larger decimal points than Float supports.
Its default value is 0.0, size: 64-bit, range varies from 2.3*10-308 to 1.7*10308 (~15 digits)
Double data type example:
Output:
3.1416
Character Data type
Character data type can store a single character string literal. It is used to add emoji or languages other than English.
Character data type example:
Output:
J Ĵ
String Data type
String data type can store collection of characters. It is Value type and its default value is “” (Empty String).
Swift facilitates you to use a few special escape sequences to use them in string. For example,
String Data type example:
Output:
Jack "Jack" Ĵack
Range of data type variables in tabular form
Type | Typical Bit Width | Typical Range |
---|---|---|
Int8 | 1byte | -127 to 127 |
UInt8 | 1byte | 0 to 255 |
Int32 | 4bytes | -2147483648 to 2147483647 |
UInt32 | 4bytes | 0 to 4294967295 |
Int64 | 8bytes | -9223372036854775808 to 9223372036854775807 |
UInt64 | 8bytes | 0 to 18446744073709551615 |
Float | 4bytes | 1.2E-38 to 3.4E+38 (~6 digits) |
Double | 8bytes | 2.3E-308 to 1.7E+308 (~15 digits) |