0 1 00:00:00,120 --> 00:00:00,610 Hey, guys. 1 2 00:00:00,630 --> 00:00:05,180 Welcome to another Swift Deep Dive. In this lesson, we're going to cover 2 3 00:00:05,180 --> 00:00:12,720 Swift Dictionaries, just like the array collection type. Dictionaries are another collection data type 3 4 00:00:12,750 --> 00:00:14,250 in Swift. 4 5 00:00:14,250 --> 00:00:15,770 So how do they work? 5 6 00:00:15,780 --> 00:00:22,530 Well, just as how if you'll look inside a dictionary, you'll find words and each word has an accompanying 6 7 00:00:22,650 --> 00:00:24,900 explanation or meaning. 7 8 00:00:24,900 --> 00:00:33,120 Well, in programming, we have a similar concept. So you could express this dictionary entry in code like 8 9 00:00:33,120 --> 00:00:41,250 this. So you could create a dictionary variable and set it to equal a collection, so all collections in 9 10 00:00:41,250 --> 00:00:48,750 Swift start and end with the square brackets. And as opposed to an array where we just have a whole bunch 10 11 00:00:48,750 --> 00:00:57,330 of items separated by commas, in a dictionary, we have what's called a key-value pair where the key is 11 12 00:00:57,330 --> 00:01:04,620 simply the item that we could look up, say, the words in a dictionary, and the value is the accompanying 12 13 00:01:04,620 --> 00:01:05,100 piece. 13 14 00:01:05,100 --> 00:01:09,260 So in this case, it's the definition of this word. 14 15 00:01:09,280 --> 00:01:12,990 Now, you're not limited to just a single entry per dictionary. 15 16 00:01:12,990 --> 00:01:15,270 You can have as many as you want. 16 17 00:01:15,270 --> 00:01:21,030 So for example, we can have a dictionary where we have a brewery and the definition of a brewery, and 17 18 00:01:21,030 --> 00:01:22,470 then we could have a bakery 18 19 00:01:22,470 --> 00:01:24,420 and the definition of a bakery. 19 20 00:01:24,420 --> 00:01:29,940 So we have multiple key-value pairs which are separated by the comma. 20 21 00:01:29,940 --> 00:01:37,590 Now, in this case, in my dictionary, my key and my value have the same data type that both strings, the pieces 21 22 00:01:37,590 --> 00:01:44,790 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 23 00:01:44,820 --> 00:01:45,900 as the value. 23 24 00:01:46,020 --> 00:01:51,600 For example, you could create a phonebook dictionary where you had each of your friends names and their 24 25 00:01:51,600 --> 00:01:55,710 phone numbers stored in separate key value pairs. 25 26 00:01:55,830 --> 00:02:03,740 And notice how in this case, I'm actually specifying an explicit data type, instead of using type inference. 26 27 00:02:03,750 --> 00:02:10,320 So when I create my dictionary, I specify a data type using the colon, and then I say that this is going 27 28 00:02:10,320 --> 00:02:11,850 to be a collection type, 28 29 00:02:11,880 --> 00:02:14,560 so square brackets and the key. 29 30 00:02:14,570 --> 00:02:20,340 So the first item is going to be of string data type, and then the second item, the value is going to 30 31 00:02:20,340 --> 00:02:21,870 be of integer data type. 31 32 00:02:22,230 --> 00:02:29,640 So now I can stuff as many of these key-value pairs into my dictionary as I want as long as I stick 32 33 00:02:29,700 --> 00:02:31,470 to those data types. 33 34 00:02:31,470 --> 00:02:35,510 So keys being strings and values being integers. 34 35 00:02:35,700 --> 00:02:42,180 If we think about arrays and we think about the way that we create an array, we simply stuffed a whole 35 36 00:02:42,180 --> 00:02:48,690 bunch of value separated by commas into a collection, right, into a set of square brackets. And then when 36 37 00:02:48,690 --> 00:02:55,980 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 38 00:02:55,980 --> 00:02:59,220 brackets at the end and we put in an index value, 38 39 00:02:59,250 --> 00:03:03,540 so a number that pointed to the item that we wanted from the array. 39 40 00:03:03,750 --> 00:03:10,420 So in this case, this particular line of code would actually retrieve the word "brewery." 40 41 00:03:10,650 --> 00:03:12,570 Now, how does it work for dictionaries? 41 42 00:03:13,050 --> 00:03:17,870 Well, in this case, our dictionary would be defined like so. 42 43 00:03:17,870 --> 00:03:20,950 So it's a string int dictionary. 43 44 00:03:21,090 --> 00:03:29,610 And when we want to retrieve a item out of our dictionary, we instead of adding a number, we use the key. 44 45 00:03:30,120 --> 00:03:36,150 So we would use the name of the dictionary which is called dict and then we add a set of square brackets 45 46 00:03:36,180 --> 00:03:43,860 just as we did for arrays. But now we provide the key that we want in order to retrieve the value which, 46 47 00:03:43,860 --> 00:03:49,530 in this case, this line of code would be equal to 07712345678. 47 48 00:03:51,270 --> 00:03:56,930 So now that you've learned about dictionaries, complete the Dictionary assignment.