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 ("JavaTpoint") 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.
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
Boolean Data type example:
Integer Data type
Integer Data type example:
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
UInt Data type
UInt (Unsigned Integer) can only store unsigned numbers (positive and zero). Other remaining properties are same as Integer data type means:
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:
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:
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:
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:
Jack "Jack" Ĵack
Range of data type variables in tabular form