Collections and Control Flow - Dictionaries

Cracking on with the Collections and Control Flow course - yes I'm continuing the momentum! Will hopefully be able to stay ahead of target and complete this (well, recap and go over!) course before the weekend!

Dictionaries - Introduction

The main distinction between these and arrays is that dictionaries are unordered. The example that Pasan gives is with airports e.g. DXB: Dubai. As these are both strings, they will need speech marks around each of them. So the key-value pair is a string-string pair. The syntax is straightforward. A colon is used to separate the key from the value and each key-value is separated with a comma.

Now in the results bar, the order is not the same! This proves that the dictionary is an unordered set. The order can also change - the reason is because an order is not needed! Only use a dictionary if the order is not important. You can make it more literal by putting in the String:String key:value pair in the square brackets; this is again all part of syntax. Hovering the mouse over the name of the variable/constant, Xcode will tell you what type of collection it is and the key:value pairing.

You can use different types for the key:value pair e.g. string:int or int:bool etc.

Modifying a Dictionary

I've used a different example to Pasan - not to be obstinate, but I find it easier to learn!

var f1Drivers = ["McLaren": "Alonso", "Ferrari": "Vettel", "Red Bull": "Ricciardo"]


f1Drivers["McLaren"] = "Vandoorne"

So the syntax is to keep the key within the brackets, and the value without. 

There is an inbuilt method in Xcode - like in Swift - for updating values...

f1Drivers.updateValue("Verstappen", forKey: "Red Bull")

So, as before with arrays, it's useful to make sense of these methods. 

f1Drivers["Ferrari"] = nil

By assigning 'nil' to the value of "Ferrari", it means I want that to be worth nothing - to be removed. This can be done with the inbuilt method on Xcode, e.g.:

f1Drivers.removeValue(forKey: "McLaren")


Dealing with Non-Existent Data

An aspect worth considering is what to do if the data you are trying to modify doesn't actually exist! If you try to retrieve a value using a key, you don't just get the value back! This links to optionals. 

An optional is a type that can contain a value, or can contain nil. 


let possibleDriver = f1Drivers["McLaren"]

An optional can handle the absence of data, whereas without one, the program would crash! 

Arrays will always crash if you use an index value that does not exist. Dictionaries will not, they keys return an optional value of nil, if there is no value, or whatever the value is. This is obviously a lot safer!

let survivingDriver = f1Drivers["Red Bull"]

In the example above, there IS a value, whereas in the one above that, there is 'nil'. Either way, the program does not crash!

The challenges were all good; I'm getting better at doing them without checking any code that I've been typing out on Playground, alongside the Treehouse videos. If I do make an error, then it's no problem as I can go back to the code and check/edit it. This will be all for today - it's much healthier and better to do half an hour here and there than a huge session of a few hours to make up for what I could have done! Tomorrow/next time, I will be moving on to loops...

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)