iOS

iOS Lesson 10 – Dictionaries in Swift

We are going to cover and practice the core principle when it comes to collections in swift, Dictionaries in Swift.

Open up your Xcode and create a new playground. We are going to call this one “DictionariesInSwift” and delete the Hello world line as we don’t need it.

Dictionaries in Swift:

Dictionaries in Swift are types of collection in which data is stored in an unordered key-value association. One thing that makes it different from other collection is that they have to be explicitly defined which type of collection it will store inside its key-value pair.

There are three main collection types in swift, we have already gone through arrays. Then, we have got a set that has only one type of value that means the value needs to be unique but they are not ordered but they are unique. Then we have got Dictionaries. You are going to use dictionaries and arrays more than sets.

As I’ve mentioned before. Dictionaries have keys and values. Where the key is of a certain type then there is a value that is related to that key.one thing about the dictionary is that the key must always be unique. This is not ordered. There are very complex and efficient algorithms working under the hood that help you retrieve and fetch items.

Recap:

  • Array is an ordered list of items
  • Set has items that have only one type of values, values need to be unique.
  • Dictionaries have keys and values where keys must be unique. It is or ordered.

Let’s get started with the code. In your Xcode, we are going to start with the dictionary. Now let’s say that you have the students database in which every student has a unique roll number or examination number. Logically, the student name can be the same but the roll number or the examination number cannot be same. Dictionary is perfect to achieve this thing. Let us write down our first dictionary.

Code:

var studentDatabase = [Int:String]() // Integer type for roll number and String for student name.

in this line of code, we have to use the var keyword to define a variable. Then we give it a name. and then in the values section, we are defining two things. Integer type will be the key and String type will refer the value. In our case, we have defined a dictionary (Implicitly) that will have integers as its key and String as its value. Where Integers will represent the roll numbers and string will represent the name.

Code:

studentDatabase[01] = "Jerry" // student with roll # 01 is jerry
studentDatabase[02] = "Tom"   // student with roll # 02 is Tom      

here we are giving two values inside our dictionary in which Jerry has roll # 01 and Tom has Roll # 02.now, you can create the dictionary that can contain Key and values of different types.  The idea behind a dictionary is that there is a unique key and there is a value that can be same as any other value inside a dictionary. Just like a classroom where students can have same names but they cannot have same Registration numbers. This is the advantage of a dictionary that the keys have to be unique.

Now, if you want to clear out a dictionary then you can just write

studentDatabase = [:]

this is going to clear out all your keys and all your values.

We can also initialize the dictionary on the same line that we are defining its name and type.

var otherDatabase : [Int:String] = [01:"Tommy",02:"Mike"]

here we have defined it explicitly that the variable is of type dictionary which has Integer as its key and String as its values. In the same line, we have defined that key “01” has the value of “Tommy” and key “02” has the value of “Mike” the colon”:” separates the key from the value. There are many methods that come under the hood with dictionaries. We can access it using the dot keyword after dictionary name. we can also use .RemoveValue() method to remove value at a certain index.

otherDatabase.removeValue(forKey: 01)       

Now, if we use .count() method we will see the total number of items inside it. We will see one element instead of two.

Dictionaries in Swift Example

We can add an item to the dictionary as long as it has a unique key. if we give it the same key, it will override the current value

otherDatabase[03] = "Geeks"
otherDatabase[04] = "Creatives"

Added two new entries in the dictionaries with unique keys. You can also iterate through a dictionary.

for (databaseKey,databaseValue) in otherDatabase{

print(otherDatabase[databaseKey])

}

we can use a for-in loop to iterate through the key database. Right now we have set (databaseKey, databaseValue) to refer key and value of the dictionary.

We have different properties in dictionaries to make our life very simple and easy while working with.

  • .keys will return all the keys available in the dictionary.
  • .values contain all the values from the dictionary.

Dictionaries in Swift Code

Conclusion:

So in this knowledge bit, we have covered Dictionaries. We have seen few examples how dictionaries work and how we can iterate through dictionaries. You can get all the code from here.

You can have a look at our whole IOS series as well.

Click to comment

Most Popular

To Top