Learning Swift : Types

 

Continuing from where we left in Variables and Constants. Let’s talk in detail about the data types in Swift.

Swift comprises of the following data types :

    1. String

    2. Character

    3. Integer

    4. Double

    5. Floating

    6. Boolean

    7. Optional

 

The source code of this post is available at Learning Swift – Types

Lets analyze each of them one by one.

String

Strings represents collection of characters, words or sentences. Example :

 

var firstName : String = "John"
print(firstName)
var fullName : String = "John Dang"
print(fullName)

 

The output will be John and John Dang respectively

 

Character

Character represents single alphabet or number or special symbol. One cannot assign a word or sentence to the character constant or variable. Example :

 

var alpha : Character = "A"
print (alpha)
var beta : Character = "i"
print (beta)

 

The output will be “A” and “i” respectively.

 

Integer

Integer represents whole numbers. Int in Swift is used to represent signed whole numbers and UInt is used to represent unsigned whole numbers. Example : 

 

var age : Int = 89
print (age)

var temp : Int = -78
print (temp)

var numberOfOranges : UInt = 10
print(numberOfOranges)

var numberOfApples : UInt = -100

Output

89
-78
10

 

As you can see Int is used for both positive and negative integers 89 and -78. But if you assign -100 to a variable of type UInt, you will get an error like this : “Negative integer ‘-100’ overflows when stored into unsigned type ‘UInt’”

 

UInt error

 

Float and Double

Float and double are used to represent the fractional numbers. Float is used for 32 bit floating numbers while Double is used for 64 bit floating numbers. Example :

 

var weight : Float = 78.9
print(weight)
var height : Double = 9.67786
print(height)

The output will be 78.9 and 9.67786 respectively

Boolean

Boolean represent true/false values. Example :

 

var light : Bool = true
print(light)

 

The output will be true.

 

We will be analyzing the optional types in our next post. Before rounding up let’s delve into Type Safety and Type Inference features of Swift.

 

Type Safety

 

Take a look into the following example to understand this concept :

 

var dob : Int = 14
dob = "Hello My date of birth is "

 

You will receive the error : “Cannot assign value of type ‘String’ to type ‘Int’”

 

type safety error

 

Swift makes sure that we never assign value of one type to a variable of another type. As we can see in the above example, when we try to assign value of String type to the variable dob which is of Integer type, an error occurs.

This is true even when we try to assign value of Integer type to a variable of String type. You get the same error as above.

 

var id = "A67"
id = 89

 

error type safety

Type Inference

Take a look at this example :

 

var score = 14
print (score)

 

We well receive an output of 14 with no error even when we haven’t defined the type of variable score. This happens because swift automatically derives the type of variable as Integer with the help of its value 14.

 

If you have any doubts, do post them in the comment section below.