1 00:00:01,130 --> 00:00:06,660 So will come back again in this lesson we're going to continue with the collections in Objective C and 2 00:00:06,660 --> 00:00:11,240 we will be talking about a particular type of collection called a dictionary. 3 00:00:11,520 --> 00:00:16,050 Dictionaries are essentially a key value pairs collection. 4 00:00:16,050 --> 00:00:20,980 What it means is imagine if you have an array and then every element in the array. 5 00:00:21,040 --> 00:00:28,140 So of having an index that has a numeric value it has an actual title that you can search for that title 6 00:00:28,230 --> 00:00:31,570 and find that a specific item in your array. 7 00:00:31,570 --> 00:00:39,030 The action is essentially the same thing that they allow you to have a key value pair or objects within 8 00:00:39,090 --> 00:00:40,260 a collection. 9 00:00:40,260 --> 00:00:47,280 So that's going to ex-cult next for them when I close this project I'm going to start a new one say 10 00:00:47,310 --> 00:00:48,810 command line tool. 11 00:00:48,990 --> 00:00:58,140 This is dictionaries project so you know dictionaries project on regular started with my very first 12 00:00:58,530 --> 00:00:59,200 dictionary. 13 00:00:59,220 --> 00:01:03,720 So let me get some better formatting in here as usual. 14 00:01:04,020 --> 00:01:10,590 And then the first thing I'm going to do is that I'm going to say I have a.. 15 00:01:10,600 --> 00:01:12,220 Dictionary. 16 00:01:12,300 --> 00:01:22,680 This is going to be my first dictionary that's going to be and this dictionary dictionary it up just 17 00:01:22,680 --> 00:01:23,620 for me. 18 00:01:23,700 --> 00:01:26,630 So I'm going to say the opposite that I have. 19 00:01:26,640 --> 00:01:36,330 It's going to be my name and the key for it is going to be name G or anything like that. 20 00:01:36,330 --> 00:01:42,210 So right now I have a dictionary vaire an object that is content. 21 00:01:42,250 --> 00:01:46,810 This here is associated with a key called the name key. 22 00:01:46,830 --> 00:01:58,590 So if I want to find out I could go ahead and say and Aslaug let's say a value for name is and whatever 23 00:01:58,590 --> 00:02:08,790 object you find the later on and that value is going to be my first dictionary object for key and the 24 00:02:08,790 --> 00:02:13,940 object for a key is the key I'm looking for is name me. 25 00:02:14,580 --> 00:02:22,570 Let's run this or this project is going to do it is going to say let's go to my first dictionary search 26 00:02:22,570 --> 00:02:28,400 for a key and that key is Name Key and find the value for it. 27 00:02:28,400 --> 00:02:29,790 I'm printed in here. 28 00:02:29,860 --> 00:02:35,980 Now keep in mind that the keys are arbitrary values they don't have to be anything specific if you want 29 00:02:35,980 --> 00:02:37,310 call this in. 30 00:02:37,320 --> 00:02:42,970 And obviously when you're searching the search for a name if you want to call it name then you know 31 00:02:43,030 --> 00:02:45,160 call that one name and this one name. 32 00:02:45,160 --> 00:02:51,190 I usually tend to finish them with a tidy key so I know that these are my keys. 33 00:02:51,340 --> 00:02:53,750 And the other ones are my values. 34 00:02:53,770 --> 00:02:59,910 Now while this might be useful it's very difficult to enter values like this. 35 00:02:59,920 --> 00:03:06,610 So instead of having a dictionary with a set number of ideas they actually usually make this mutable 36 00:03:06,610 --> 00:03:08,410 dictionary and be cheap. 37 00:03:08,410 --> 00:03:09,870 Adding values to it. 38 00:03:10,060 --> 00:03:12,740 Let's see how we're going to do that. 39 00:03:12,760 --> 00:03:18,330 So now I'm going to say and it's mutable dictionary. 40 00:03:18,580 --> 00:03:27,760 This is going to be my mutable dictionary that's going to be and that's mutable dictionary oarlock and 41 00:03:27,850 --> 00:03:31,030 in it it is allocated on initialize. 42 00:03:31,030 --> 00:03:33,670 So now there is a mutable dictionary. 43 00:03:33,670 --> 00:03:41,410 It is declared it is located initialize and it can take in values such as what I would say might mean 44 00:03:41,410 --> 00:03:51,700 a dictionary are a set value and the value is going to be my name for the key name key and then I'm 45 00:03:51,700 --> 00:04:02,020 going to say again my mutable my mutable dictionary sets a value and the value is Vancouver and set 46 00:04:02,150 --> 00:04:05,920 for a city XI. 47 00:04:06,350 --> 00:04:16,840 And let's have one more my mutable dictionary and sets a value and this value is going to be I guess 48 00:04:17,560 --> 00:04:23,340 on this one is going to be force Name Key. 49 00:04:23,350 --> 00:04:30,870 So now I have a dictionary that has three different keys and each key obviously has a certain value. 50 00:04:30,940 --> 00:04:39,010 And if I want to find any of those I could simply go ahead and say let me copy the entire thing our 51 00:04:39,340 --> 00:04:49,690 name or use is for instance name of use he is under I would say my immutable dictionary object for key 52 00:04:49,780 --> 00:04:51,420 on the name key. 53 00:04:51,490 --> 00:04:55,060 I have to make sure that the keys I use are existing in here. 54 00:04:55,180 --> 00:04:57,350 Otherwise there won't be any result. 55 00:04:57,350 --> 00:05:00,680 It will then give me an error or it just wouldn't print anything. 56 00:05:00,880 --> 00:05:03,040 So let's bring that out. 57 00:05:03,420 --> 00:05:06,170 Purrfect that's the first one in here. 58 00:05:06,430 --> 00:05:07,920 And this is the second one. 59 00:05:08,020 --> 00:05:16,920 Let's print the city of the city of that is for city key. 60 00:05:19,010 --> 00:05:27,710 Perfect Now as you can imagine in a larger dictionary such as this one there is 3-D Frenchy's and there 61 00:05:27,720 --> 00:05:34,670 are also to different values you could actually access those and put them in an array so I could say 62 00:05:35,300 --> 00:05:48,010 and Aslaug on a cheese bar and then whatever you find in my mutable dictionary all cheese. 63 00:05:48,110 --> 00:05:52,670 So what is going to do is going to actually produce all of the kids put them in an array. 64 00:05:52,850 --> 00:05:54,610 And then it's going to print them for me. 65 00:05:54,770 --> 00:06:00,200 And the reason I know it's going to be an array is that because when I write all the keys it tells me 66 00:06:00,470 --> 00:06:05,230 it's not all in this array it's going to return and it's way for me. 67 00:06:05,230 --> 00:06:06,710 So all keys. 68 00:06:06,980 --> 00:06:16,260 And similarly I could do the same thing for all the values I'm going to say or the values or values 69 00:06:16,310 --> 00:06:25,000 or run this so what we have done here is you obviously have the two prints that you had before. 70 00:06:24,990 --> 00:06:25,750 A nice thing. 71 00:06:25,770 --> 00:06:33,690 All keys are our main city of course Name Key and then all the values are in Vancouver. 72 00:06:33,740 --> 00:06:37,220 I was just like what we expect. 73 00:06:37,410 --> 00:06:43,680 Now if for any reason you don't want to use a mutable dictionary and you want to define and declare 74 00:06:43,680 --> 00:06:46,980 everything in one go you could actually do without us. 75 00:06:47,070 --> 00:06:52,540 So I could go ahead and say r and s dictionary are. 76 00:06:52,560 --> 00:06:55,870 Let's call this one multiply. 77 00:06:56,140 --> 00:07:02,970 You use the dictionary is in this dictionary dictionary. 78 00:07:03,260 --> 00:07:07,420 The last time we use a dictionary with object for key. 79 00:07:07,490 --> 00:07:14,370 Now this time I'm actually going to use dictionary with objects and keys proof multiple objects and 80 00:07:14,370 --> 00:07:15,550 multiple keys. 81 00:07:15,600 --> 00:07:17,790 So the way you write those is like this. 82 00:07:17,790 --> 00:07:20,460 I'm going to say actually with objects and keys. 83 00:07:20,670 --> 00:07:28,290 Every first value every first entry will be considered a value on every second one with because a key. 84 00:07:28,290 --> 00:07:34,880 So essentially right now there is one key called K for one value called the V. 85 00:07:35,040 --> 00:07:38,570 Now let's try to remake this thing that we have up there. 86 00:07:38,670 --> 00:07:48,900 I'm going to say it is a mere The name key and it's a good practice to actually not bring this one line 87 00:07:48,900 --> 00:07:52,060 down so we can see them properly next to each other. 88 00:07:52,410 --> 00:08:01,710 And then I'm going to say the next one is code word for City Name Key and then I think the last one 89 00:08:01,710 --> 00:08:09,570 with the I O S on this with the horse name keep perfect. 90 00:08:09,750 --> 00:08:16,500 So this is not a dictionary that the initialization Lebaran initialising you're putting up multiple 91 00:08:16,500 --> 00:08:17,810 values in it. 92 00:08:17,850 --> 00:08:20,720 So this is one key value. 93 00:08:20,730 --> 00:08:24,250 This is another key value and this is another key value. 94 00:08:24,360 --> 00:08:31,140 And just like the previous examples I could just go ahead and say print a certain thing from here say 95 00:08:31,200 --> 00:08:38,450 and I like my R value for either no. 96 00:08:38,730 --> 00:08:40,780 Of course he is. 97 00:08:40,830 --> 00:08:49,800 And then when I say on and then I'm going to say multiple values dictionary object for key and the key 98 00:08:49,800 --> 00:08:54,580 I'm looking for is horse name Keith. 99 00:08:55,280 --> 00:08:58,520 That's wrong and if it all goes well it should print. 100 00:08:58,530 --> 00:08:59,940 I was at the very end. 101 00:08:59,970 --> 00:09:07,740 Yes value for course is I always know that because the sense of how dictionaries are built we have to 102 00:09:07,740 --> 00:09:15,950 talk about something quite important that is missing dictionaries within arrays or arrays within dictionaries. 103 00:09:16,050 --> 00:09:22,180 So to do that I'm actually going to comment out all of my N.S. logs so we don't see a lot of stuff here. 104 00:09:22,320 --> 00:09:25,440 I'm going to go ahead and say Karmen this one out. 105 00:09:25,440 --> 00:09:27,110 Carmen Carmen Carmen. 106 00:09:27,230 --> 00:09:32,670 And if you're wondering how do I do this common thing is command and forward slash. 107 00:09:32,700 --> 00:09:38,040 It's a hot key for commenting out anything that is in the current line. 108 00:09:38,040 --> 00:09:44,520 So if we select these guys and say command forward slash it Carmens all of them out and if they do it 109 00:09:44,520 --> 00:09:47,560 again it essentially on common stuff. 110 00:09:47,610 --> 00:09:53,280 So now when we run our application or run successfully What we don't see anything down there because 111 00:09:53,280 --> 00:09:55,420 we are not printing any values. 112 00:09:55,500 --> 00:10:02,100 So in this example I want to do this I want to put two users inside one array. 113 00:10:02,280 --> 00:10:09,830 So once again to use users inside one array and every user has only two entities name and city. 114 00:10:10,050 --> 00:10:11,720 So just go ahead and do that. 115 00:10:11,730 --> 00:10:16,040 I'm going to say N.S. dictionary use. 116 00:10:16,430 --> 00:10:22,630 One dictionary is going to be an ass dictionary dictionary. 117 00:10:22,740 --> 00:10:27,270 Objects and keys and the objects are these. 118 00:10:27,330 --> 00:10:33,240 And you know that his name key just like what they have done before. 119 00:10:33,290 --> 00:10:43,770 I have the other user which is the other piece of information which is Vancouver and that is for a city 120 00:10:44,280 --> 00:10:50,000 or city she and that's one of my users. 121 00:10:50,040 --> 00:10:52,570 Let's duplicate this once again. 122 00:10:52,800 --> 00:11:00,930 And the copy I'm going to say use to dictionary this is going to be Steve. 123 00:11:01,260 --> 00:11:07,160 Steve lives in either North San Francisco. 124 00:11:07,170 --> 00:11:09,350 If that's how you spell it so good. 125 00:11:09,350 --> 00:11:13,250 Two dictionaries each of them about one of my users. 126 00:11:13,380 --> 00:11:16,540 And now I want to put both of them inside an array. 127 00:11:16,670 --> 00:11:22,420 So I'm going to say and this mutable array Let's call this one user's array. 128 00:11:22,430 --> 00:11:27,170 Is this N.S. mutable array lock and in it. 129 00:11:27,460 --> 00:11:31,720 And that's not a bad time to mention this whenever you have to do this. 130 00:11:31,740 --> 00:11:33,060 I look in it. 131 00:11:33,100 --> 00:11:35,750 You can do only one and that is new. 132 00:11:35,770 --> 00:11:40,830 So this is a lot in it I could say just do you. 133 00:11:40,840 --> 00:11:48,450 Which is exactly identical to saying aloud and then saying in it. 134 00:11:48,490 --> 00:11:51,060 So this too would be exactly identical. 135 00:11:51,220 --> 00:11:57,990 So let's move to our users array and I'm going to say use as array as the object. 136 00:11:57,990 --> 00:12:00,690 Use one dictionary. 137 00:12:00,910 --> 00:12:08,320 And then I'm going to say use array and object and now add the user to dictionary. 138 00:12:08,410 --> 00:12:17,470 So most of my dictionaries are now added inside my array and if I print my array and log whatever object 139 00:12:17,470 --> 00:12:20,560 you find in the user's array. 140 00:12:20,740 --> 00:12:26,460 If you print this we should be able to see the entire thing in one big array. 141 00:12:26,710 --> 00:12:29,750 So just like Will we expect that this is the array. 142 00:12:29,980 --> 00:12:34,210 So that is the array that you Panthers these are the two round brackets. 143 00:12:34,390 --> 00:12:39,150 Within that there is one object column or the other object. 144 00:12:39,220 --> 00:12:45,940 And as we expected within that curly brackets which is our dictionary there is the city of Vancouver 145 00:12:45,950 --> 00:12:47,260 named Amir. 146 00:12:47,470 --> 00:12:53,220 And then for the other use the city key sanfrancisco name key Steve. 147 00:12:53,490 --> 00:13:00,250 Now in the next few lessons when we talk about flu we will learn how to search between all of these 148 00:13:00,250 --> 00:13:03,000 elements and find exactly what we want. 149 00:13:03,070 --> 00:13:06,340 But for now I just want to show you something real quick. 150 00:13:06,340 --> 00:13:13,750 Imagine we wanted to go and find our second array object in the array and from that we wanted to get 151 00:13:13,840 --> 00:13:15,420 the name of the user. 152 00:13:15,460 --> 00:13:23,290 So I'm going to say name of their second are our dictionary. 153 00:13:24,070 --> 00:13:35,920 Does the name here I would say on one line down I would say use Array object that index 1 that is the 154 00:13:35,920 --> 00:13:36,730 second one. 155 00:13:36,730 --> 00:13:40,600 Remember arrays start from 0 0 1. 156 00:13:40,750 --> 00:13:43,660 So now we got that first dictionary. 157 00:13:43,660 --> 00:13:52,570 Then I'm going to open another bracket and then close up and say object for CI and they keep I'm looking 158 00:13:52,570 --> 00:13:54,720 for is name key. 159 00:13:55,030 --> 00:14:00,760 So it's essentially saying if you wanted to see it visually is saying go to the user's array in the 160 00:14:00,760 --> 00:14:09,430 users or go to the object that index one or not to this one but this one in here says one object for 161 00:14:09,430 --> 00:14:13,450 the key name key and that's the name key. 162 00:14:13,610 --> 00:14:14,750 Are printed. 163 00:14:14,950 --> 00:14:17,000 So let's see what we get. 164 00:14:17,140 --> 00:14:28,500 Run that I sing name off the second dictionary is Steve or a dad naming name for the second use I should 165 00:14:28,500 --> 00:14:35,430 have said so I can use a name for this I can use that is Steve. 166 00:14:35,490 --> 00:14:37,870 Just like how you expected it to be. 167 00:14:37,920 --> 00:14:39,480 So that's about that. 168 00:14:39,510 --> 00:14:44,860 Dictionaries are of value key pair collections. 169 00:14:44,970 --> 00:14:52,830 You can have so many different keys and so many different values inside the dictionary to find a certain 170 00:14:52,830 --> 00:14:54,820 value within a certain dictionary. 171 00:14:54,930 --> 00:14:57,120 We look for object for key. 172 00:14:57,120 --> 00:15:04,740 We get that we can also get things such as all the keys and we can get all the values there is more 173 00:15:04,740 --> 00:15:10,950 of these methods such as getting all the values for a certain key or getting all the kids that correspond 174 00:15:10,950 --> 00:15:12,000 to this value. 175 00:15:12,060 --> 00:15:18,180 What are those things where can see them as we go along and we actually you know use dictionaries inside 176 00:15:18,180 --> 00:15:19,320 our applications. 177 00:15:19,470 --> 00:15:27,780 For now just learn how to essentially initialize dictionaries either with a single key value pair or 178 00:15:27,780 --> 00:15:34,790 with a mutable matter that you can keep adding new values to them or with multiple values in one go. 179 00:15:34,920 --> 00:15:42,020 Obviously about nesting in different dictionaries inside an array which is a very common practice. 180 00:15:42,150 --> 00:15:47,820 And of course finding values inside them getting the array from the array. 181 00:15:47,820 --> 00:15:51,080 Getting that particular object that is a dictionary. 182 00:15:51,090 --> 00:15:57,090 And from that dictionary getting the object for a particular cheat would be essentially what you would 183 00:15:57,090 --> 00:15:57,830 need to do. 184 00:15:58,110 --> 00:16:00,180 So that's about that. 185 00:16:00,180 --> 00:16:02,880 Let's keep it up to here on in the next lesson. 186 00:16:02,880 --> 00:16:04,230 We'll move on. 187 00:16:04,290 --> 00:16:07,890 I treasons on for loops on control froths.