Swift : Programming Language

Data Types

In any programming language we write software code to perform some calculation. These calculations are carried out on some data. Those data are in nature of some number or string.

Numbers are usually integers or fractional. Example: 1, 2, 3, 1.45, 4.382

Strings are a collection of alphabetic letter and symbols. Example: Hello Mr. Pawan.

Now how do we store these information? We have seen in last tutorial we have immutable data and mutable data but we have not seen different type of data there. Lets learn them.

We have some basic data types, which are also called primitive data types, provided by swift programming language as follows:

We also have composite data types or secondary data types provided by swift as follows:

We also have various collection class which helps us to store many data using a common name as collection and helps us to access them easily when we need. They are as follows:

Basic Data types:

Int : Signed Integer represented in 2s complement. Swift provides variants of Int as Int8, Int16, Int32 and Int64. If you don't mention any size then size of Int is 32 bit on 32 bit machine and 64 bit on 64 bit machine.
Example: -3278, 3973, 9172, 0, etc

Uint: Unsigned Integer. Swift provides variants of UInt as UInt8, UInt16, UInt32 and UInt64. If you don't mention any size then size of UInt is 32 bit on 32 bit machine and 64 bit on 64 bit machine.

Float: Fractional numbers It is 32 bit floating / fractional number. It has precision of at least 6 decimal digits.

Double: It is 64 bit floating / fractional number. It has precision of at least 15 decimal digits.

Data Type Size Range
Int8 8-bit, Signed -128 to 127
Int16 16-bit, Signed -32768 to  32767
Int32 32-bit, Signed -2147483648 to 2147483647
Int64 64-bit, Signed -9223372036854775808 to 9223372036854775807
UInt8 8-bit, Unsigned 0 to 255
UInt16 16-bit, Unsigned 0 to 65535
UInt32 32-bit, Unsigned 0 to 4294967295
UInt64 64-bit, Unsigned 0 to 18446744073709551615
Int 32-bit, Signed on 32 bit machine 64-bit, Signed on 64 bit machine Similar to Int32 or Int64 depending on machine's data size.
UInt 32-bit, Unsigned on 32 bit machine 64-bit, Unsigned on 64 bit machine Similar to UInt32 or UInt64 depending on machine's data size.
Float 32-bit, Signed, 6 decimal digit precision --
Double 64-bit, Signed, 15 decimal digit precision --
Note: There is no concept of long here in swift.

Lets try some code in playground [ hope you remember playground :) ] and see what result you get in quick view interactive console.

let maxValue8: Int8 = Int8.max
let minValue8: Int8 = Int8.min
let maxValue16: Int16 = Int16.max
let minValue16: Int16 = Int16.min
let maxValue: Int32 = Int32.max
let minValue: Int32 = Int32.min
let maxValue64: Int64 = Int64.max
let minValue64: Int64 = Int64.min
let uMaxValue8: UInt8 = UInt8.max
let uMinValue8: UInt8 = UInt8.min
let uMaxValue16: UInt16 = UInt16.max
let uMinValue16: UInt16 = UInt16.min
let uMaxValue: UInt32 = UInt32.max
let uMinValue: UInt32 = UInt32.min
let uMaxValue64: UInt64 = UInt64.max
let uMinValue64: UInt64 = UInt64.min

Composite data types:

Character:  Character is a storage for single unicode symbol or letter. Example: "🐶 " or "🐱 " or "C" or "!"

String: A string is a series or array of characters.
Example: "Hello", "Ram earned Rs. 5000 from his investment"
Note: We can do many operations on string that will be covered latter.

Tuple: Tuple as name says it provides storage for a list of elements.
Example (40, "Pawan", "Bangalore, India").
Now again lets jump to playground and start checking how does these work.

//Character example
let p: Character = "P"
let a: Character = "A"
let w: Character = "W"
let n: Character = "N"
print ( p, a, w, a, n, separator: "", terminator: "" )

Output on console:

//String Example
let hello = "Hello" let mr = "Mr." let pawan = "Pawan"
print ( hello, mr, pawan )

Output on console:
Hello Mr. Pawan

//Tuple Example
let student = (40, "Pawan", "Bangalore, India")
print ( student )

let (roll, name, address) =  student
print ( address )

Output on console:
(40, "Pawan", "Bangalore, India") Bangalore, India

Collection Classes:

Array: Array is a collection similar objects in an ordered list.

Example: ["Pawan","Ramesh","Shilpi","Suresh", "Sushma", "Tushar"]
The above example shows the names of students in a class.

Set: Set is similar to array, so it is also the collection of similar objects but it does not store same objects again in the collection. Also set collection is unordered.

Example: ["Pawan","Ramesh","Shilpi","Suresh", "Sushma", "Tushar"]

Lets understand what are the differences between Array and Set here.

  1. Ordering - If you have an array as above then it starts at index 0 and then continues. So if I want to read any data stored at index 0 we will get "Pawan" here but in case of set we won't even though we stored data in same order.
  2. Duplication - In set you can't store same object again and again but in an array you can do. Say in a class there are two students with same name as "Shilpi" which is a very common name in India. You won't be able to store it in set but in array you can.

Dictionary: Dictionary is type of storage where a collection having values associated with some key is stored in an unordered manner.

Example: [4:"Pawan",5:"Ramesh",7:"Shilpi",9:"Suresh",10:"Sushma",13:"Tushar"]
Above example stores the values as names of students and keys as their roll numbers.

All these topics will be discussed in detail again in Collection Classes. Warmup time with some play with code on playground. Join me there in playground :)

//Array example
let studentsArray:Array = ["Pawan","Ramesh","Shilpi","Suresh","Sushma","Tushar"]
print (studentsArray)
print (studentsArray[0], studentsArray[2])
print (studentsArray[1], studentsArray[3])

Output on console:
["Pawan", "Ramesh", "Shilpi", "Suresh", "Sushma", "Tushar"]
Pawan Shilpi
Ramesh Suresh

print ( studentsArray[-1] , studentsArray[20] )
Output on console:
fatal error: Array index out of range

//Set example
let studentsSet: Set = [ "Pawan", "Ramesh", "Shilpi", "Suresh", "Sushma", "Tushar" ]
print ( studentsSet)

Output on console:
["Shilpi", "Suresh", "Sushma", "Tushar", "Ramesh", "Pawan"]

//Dictionary Example
let studentsDict:Dictionary = [4 : "Pawan", 5 : "Ramesh", 7 : "Shilpi", 9 : "Suresh", 10 : "Sushma", 13 : "Tushar"]
print (studentsDict)
print (studentsDict[5])
print (studentsDict[5]!)

Output on console:
[5: "Ramesh", 7: "Shilpi", 10: "Sushma", 4: "Pawan", 9: "Suresh", 13: "Tushar"]


  1. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html