A data type is the type of data (value) a variable or constant can store in it. For example, in the article Swift Variables and Constants, you created a variable and a constant to store string data in the memory.
This data can be a text/string ("Hello") or a number (12.45) or just bits (0 &1). Defining the data type ensures only the defined type of data is stored.
Let’s look at a scenario:
Suppose you want to create a game. Since, most games display high score and player’s name after the game completes, you need to store these two data for your game.
The high score is a number (e.g. 59) and player’s name a string(e.g Jack). You can create two variables or constants to store the data.
In Swift, this can be done by declaring variables and the data type as:
var highScore:Int = 59 var playerName:String = "Jack"
Here, we declared highScore variable of type Int
which stores value 59. And, playerName variable of type String
which stores value Jack.
However, if you do something like this:
var highScore:Int = "Jack"
You will get a compile time error stating cannot convert value of type 'String' to specified type 'Int'.
It’s because you declared highScore to store integer value but placed string in it. This error ensures highScore can only store a number and not player’s name.
Another important part of a data type is its size. This specifies the size of data that can be stored in a given variable or constant.
A Type’s size is measured in terms of bit and can store values upto 2bits. If you don't know about Bit, think of it as a value that is either 0 or 1.
So, for a Type size = 1 bit, it can only store upto, 21 = 2, two values: either 0 or 1. So a 1 bit system for a letter program can interpret 0 as a/0 and 1 as b/1.0.
0 -> a or 0 1 -> b or 1
Likewise, a two bit system can store upto 22 = 4 values: (00,01,10,11) and each combination can be represented as:
00 -> a or 0 01 -> b or 1 11 -> c or 2 10 -> d or 3
For a n bit system, it can store a maximum of 2n values in it.
The most common data types used in swift are listed below:
true
or false
.if-else
statement. Swift if else article covers in detail about it.
let result:Bool = true
print(result)
When you run the program, the output will be:
true
UInt
, Int8
, Int16
etc. The most common one you use is of Int
.UInt
, Int8
, Int16
etc. which we are going to explore below.
var highScore:Int = 100
print(highScore)
highScore = -100
print(highScore)
When you run the program, the output will be:
100 -100
In the above example we declared a variable highScore of type Int
. First, we assigned it with a value of 100 so print(highScore)
outputs 100 in the screen.
Later, we changed the value to -100 using assignment operator as highScore = -100
so, print(highScore)
outputs -100 in the screen.
Let's explore some variants of Int
type in Swift.
A Int8
integer can store altogether 28 = (256) values in it. i.e it can store numbers from 0 to 255 right?
Yes, you are correct. But since, Int8
includes both positive and negative numbers we can store numbers from -128 to 127 including 0 which totals to 256 values or numbers.
var highScore:Int8 = -128//ok
highScore = 127 //ok
highScore = 128 // error here
highScore = -129 //error here
You can also find out the highest and lowest value a type can store using .min
and .max
built in Swift functions.
Example 3: Max and Min Int8 data type
print(Int8.min)
print(Int8.max)
When you run the program, the output will be:
-128 127
Example 4: UInt data type
var highScore:UInt = 100
highScore = -100//compile time error when trying to
The above code will give you a compile time error because a Unsigned Integer can only store either 0 or a positive value. Trying to store a negative value in an Unsigned Integer will give you an error.
let highScore:Float = 100.232
print(highScore)
When you run the program, the output will be:
100.232
let highScore:Double = 100.232321212121
print(highScore)
When you run the program, the output will be:
100.232321212121
let playerName:Character = "J"
let playerNameWithUnicode:Character = "\u{134}"
print(playerName)
print(playerNameWithUnicode)
When you run the program, the output will be:
J Ĵ
\0
(null character),\\
(a plain backslash \),\t
(a tab character),\v
(a vertical tab),\r
(carriage return),\"
(double quote),\'
(single quote), and\u{n}
(unicode code point ,n is in hexadecimal).
let playerName = "Jack"
let playerNameWithQuotes = "\"Jack\""
let playerNameWithUnicode = "\u{134}ack"
print(playerName)
print(playerNameWithQuotes)
print(playerNameWithUnicode)
When you run the program, the output will be:
Jack "Jack" Ĵack
See Swift characters and strings to learn more about characters and strings declaration, operations and examples.
In addition to this data types, there are also other advanced data types in Swift like tuple, optional, range, class, structure etc. which you will learn in later chapters.
1. Since Swift is a type inference language, variables or constants can automatically infer the type from the value stored. So, you can skip the type while creating variable or constant. However you may consider writing the type for readability purpose but it’s not recommended.
Example 9: Type inferred variable/constant
let playerName = "Jack"
print(playerName)
Swift compiler can automatically infer the variable to be of String type because of its value.
2. Swift is a type safe language. If you define a variable to be of certain type you cannot change later it with another data type.
Example 10: Swift is a type safe language
let playerName:String
playerName = 55 //compile time error
The above code will create an error because we already specified that variable playerName is going to be a String. So we cannot store a Integer number in it.
It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:
Disabling AdBlock on Programiz. We do not use intrusive ads.
or
Donate on Paypal