Dictionary – Swift 3

Dictionary is an another container/collection type in Swift.This tutorial gives the basic understanding of Dictionary or key value type.  Dictionary and Set both collection types are unordered types whereas Array is an ordered type, items in an array are in an order. Like String or Array, Dictionary tutorial also briefs initialization, declaration, accessing and modifying the dictionary.

“Dictionary is a key value pair data type, each value in a dictionary is associated with a unique value”. This key helps to access the value from the dictionary. And the items or elements in a dictionary can be in any order.

Dictionary Initialization:

Just like other types in Swift, Dictionary also has inferred and explicit declaration.

Below example does not specify its key, value type explicitly. Hence this type of declaration is inferred.

let inferredDict = ["gender" : "male" , "name" : "ProgramTeach"]

 The above declaration initializes the dictionary with two items. As both values in the dictionary are a String type, the compiler is not prompting the error to specify the type. let’s add age with Integer type value in the sample.

let inferredDict = ["gender" : "male" , "name" : "ProgramTeach" , "age" : 21] as [String : Any]

if you see the colored casting is to let the compiler know the type is dictionary having generic values. The below declaration is equal to that where we are explicitly specifying the Generic type in front.

let inferredDict :  [String : Any] = ["gender" : "male" , "name" : "ProgramTeach" , "age" : 21]

Dictionary Mutability:

Mutability is same as we have NSMutableDictionary and NSDictionary in Objective C. If a dictionary declared with let keyword, it is constant and its values can not be modified. The above samples are declared with let keyword. If a dictionary is declared with var keyword, it is mutable and value of that dictionary can be modified, new key value can also be inserted.

var inferredDict :  [String : Any] = ["gender" : "male" , "name" : "ProgramTeach" , "age" : 21] // now the keyword is var. 

We will extend this in next section called accessing / Modifying the dictionary:

Accessing a Dictionary:

As we said earlier, a unique key is to access the value from a dictionary as a subscript.

The returned value is optional since it may have value or not, Below optional binding helps to check value presence.

if let age = inferredDict["age"]{

    print(age)

}

else {

    print("age is not there")

}

There we are reading the age from that dictionary, if it has value for that key, it gives value.

Modifying a Dictionary:

Dictionary with var keyword is mutable. We can add, update or delete an item from the dictionary.

var firstDict :  [String : Any] = ["gender" : "male" , "name" : "ProgramTeach" , "age" : 21]

firstDict["age"] = 31 // updates the age

firstDict["address"] = "ProgramTeach , no 6 xyz street" // adds new entry to that dictionary

firstDict.removeValue(forKey: "gender") // removes an item gender from dictionary.

Empty Dictionary:

Empty dictionary means instance with zero entries that can be identified using isEmpty property.

if firstDict.isEmpty {

    print("empty dictionary")

}

To create empty Dictionary,

let emptyDict : Dictionary<String,String> = Dictionary<String,String>()

let shortEmptyDict = [String:String]() // short form

Playground screenshot for covering this tutorial.

Swift Dictionary

Please share and drop your thoughts in comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *