Hey, guys. 1
Welcome to another Swift Deep Dive. In this lesson, we're going to cover 2
Swift Dictionaries, just like the array collection type. Dictionaries are another collection data type 3
in Swift. 4
So how do they work? 5
Well, just as how if you'll look inside a dictionary, you'll find words and each word has an accompanying 6
explanation or meaning. 7
Well, in programming, we have a similar concept. So you could express this dictionary entry in code like 8
this. So you could create a dictionary variable and set it to equal a collection, so all collections in 9
Swift start and end with the square brackets. And as opposed to an array where we just have a whole bunch 10
of items separated by commas, in a dictionary, we have what's called a key-value pair where the key is 11
simply the item that we could look up, say, the words in a dictionary, and the value is the accompanying 12
piece. 13
So in this case, it's the definition of this word. 14
Now, you're not limited to just a single entry per dictionary. 15
You can have as many as you want. 16
So for example, we can have a dictionary where we have a brewery and the definition of a brewery, and 17
then we could have a bakery 18
and the definition of a bakery. 19
So we have multiple key-value pairs which are separated by the comma. 20
Now, in this case, in my dictionary, my key and my value have the same data type that both strings, the pieces 21
of text, but you can mix it up. You can have a dictionary where you have a string as the key and an integer 22
as the value. 23
For example, you could create a phonebook dictionary where you had each of your friends names and their 24
phone numbers stored in separate key value pairs. 25
And notice how in this case, I'm actually specifying an explicit data type, instead of using type inference. 26
So when I create my dictionary, I specify a data type using the colon, and then I say that this is going 27
to be a collection type, 28
so square brackets and the key. 29
So the first item is going to be of string data type, and then the second item, the value is going to 30
be of integer data type. 31
So now I can stuff as many of these key-value pairs into my dictionary as I want as long as I stick 32
to those data types. 33
So keys being strings and values being integers. 34
If we think about arrays and we think about the way that we create an array, we simply stuffed a whole 35
bunch of value separated by commas into a collection, right, into a set of square brackets. And then when 36
we had to retrieve an item from the array, we use the name of the array, and then we added a set of square 37
brackets at the end and we put in an index value, 38
so a number that pointed to the item that we wanted from the array. 39
So in this case, this particular line of code would actually retrieve the word "brewery." 40
Now, how does it work for dictionaries? 41
Well, in this case, our dictionary would be defined like so. 42
So it's a string int dictionary. 43
And when we want to retrieve a item out of our dictionary, we instead of adding a number, we use the key. 44
So we would use the name of the dictionary which is called dict and then we add a set of square brackets 45
just as we did for arrays. But now we provide the key that we want in order to retrieve the value which, 46
in this case, this line of code would be equal to 07712345678. 47
So now that you've learned about dictionaries, complete the Dictionary assignment.