1 00:00:01,120 --> 00:00:06,250 Either in this list then you're going to learn how to use dictionaries in swift. 2 00:00:06,340 --> 00:00:11,770 It's somehow very similar to what you have in Objective C especially from a concept perspective. 3 00:00:11,770 --> 00:00:13,500 It's exactly the same thing. 4 00:00:13,510 --> 00:00:16,270 It's just that the syntax is a little bit different. 5 00:00:16,450 --> 00:00:23,860 So let's go to X code in here and I'm going to start a new playground. 6 00:00:24,100 --> 00:00:31,540 I'm going to call this Rawn let's call it my dictionaries in the swift. 7 00:00:32,160 --> 00:00:35,260 So let's see how we got to write our first dictionary. 8 00:00:35,260 --> 00:00:37,300 So the first thing is I need a variable. 9 00:00:37,380 --> 00:00:43,270 I'm going to call it dictionary and then dictionary and what I call it to be of the type dictionary 10 00:00:43,720 --> 00:00:47,860 with values that are extreme and a string. 11 00:00:47,920 --> 00:00:54,480 This is explicitly telling dictionary what kind of values we're going to have in it. 12 00:00:54,600 --> 00:00:58,680 And then I could just go ahead and say dictionary are initialized. 13 00:00:58,710 --> 00:01:03,130 But if you want to be far more explicit which doesn't really make any difference. 14 00:01:03,190 --> 00:01:11,170 You could say explicitly or type I notated of the type the string and its thing so that's like a much 15 00:01:11,170 --> 00:01:15,600 longer the most the longest possible way of writing a dictionary. 16 00:01:15,640 --> 00:01:19,020 What if I wanted to write a lot easier I could just go ahead and say. 17 00:01:19,210 --> 00:01:21,180 My dictionary is. 18 00:01:21,400 --> 00:01:31,980 And then within brackets I would say for name key column the value is name and then column or for city 19 00:01:31,980 --> 00:01:34,270 key column. 20 00:01:34,270 --> 00:01:38,220 The value is then who were so on and so forth. 21 00:01:38,230 --> 00:01:43,060 So she value paired items. 22 00:01:43,070 --> 00:01:49,620 So right these key value pair paired items. 23 00:01:49,660 --> 00:01:57,750 Not every hair item is so simple as that I can just build my dictionary in either of the vase. 24 00:01:57,760 --> 00:02:06,140 I can go ahead and change values in them like I would say dictionary value for a something called either 25 00:02:06,310 --> 00:02:06,740 you. 26 00:02:06,770 --> 00:02:14,920 IDG unique identification make that this random you know set of characters if they you ID exist is kind 27 00:02:14,940 --> 00:02:15,990 of updated. 28 00:02:16,240 --> 00:02:23,830 If it doesn't exist essentially added I could go to my dictionary and say my dictionary value for name 29 00:02:23,830 --> 00:02:29,070 key make it to be or not. 30 00:02:29,080 --> 00:02:36,630 I mean and if I go ahead and get access to this value now you will see that when we execute our play 31 00:02:36,670 --> 00:02:39,070 playground we should see I don't mean here. 32 00:02:39,070 --> 00:02:40,020 Perfect. 33 00:02:40,060 --> 00:02:43,650 So that's exactly you know what we expected to see. 34 00:02:43,660 --> 00:02:50,460 Now the only thing that is left in here is what you play when I go through the items of my dictionary 35 00:02:50,470 --> 00:02:57,790 how could I go through every item in my dictionary to do that we need to learn a new concept something 36 00:02:57,790 --> 00:03:06,230 that is completely new in swift and it doesn't exist in Objective-C and that concept we call it a tool. 37 00:03:06,340 --> 00:03:09,700 So to pros help us do that. 38 00:03:09,700 --> 00:03:15,500 And before I actually show you how to work let me show you how they work on a dictionary. 39 00:03:15,520 --> 00:03:20,340 And then from there on we go to using it for other purposes. 40 00:03:20,350 --> 00:03:24,830 So let me take all of these out and just keep the things that I know I need. 41 00:03:24,970 --> 00:03:26,890 So that's that. 42 00:03:27,130 --> 00:03:28,680 And then I don't need a dictionary. 43 00:03:28,690 --> 00:03:31,840 I don't need that action and I don't want to update my dictionary. 44 00:03:31,840 --> 00:03:34,470 I only want to have this dictionary. 45 00:03:34,660 --> 00:03:37,550 So in my dictionary that's called my dictionary. 46 00:03:37,570 --> 00:03:38,390 I want to do this. 47 00:03:38,410 --> 00:03:43,920 I want to say four and then I'm going to say J and B. 48 00:03:44,230 --> 00:03:54,920 In my dictionary do this say trains she is interpretation of Che as the key. 49 00:03:55,000 --> 00:04:02,020 And then I'm going to copy this pasted on save value is that of course we now let's go ahead and run 50 00:04:02,020 --> 00:04:02,470 this. 51 00:04:02,560 --> 00:04:10,410 Well if I was to execute my playground we will see that it says she a name key value is a key is the 52 00:04:10,420 --> 00:04:12,400 key value is Vancouver. 53 00:04:12,520 --> 00:04:18,670 And if you have hundreds of items in our dictionary the same thing would happen if we had an array of 54 00:04:18,670 --> 00:04:19,330 dictionaries. 55 00:04:19,330 --> 00:04:24,120 It would have been the same thing you would go through the items of the array for each of them we would 56 00:04:24,130 --> 00:04:31,630 run this thing and would print every value if you wanted to find out if any of these values is equivalent 57 00:04:31,630 --> 00:04:35,950 to either know a certain name be withdrawn and if statement in the middle of this. 58 00:04:35,950 --> 00:04:41,140 Now that's all good and well what we don't know is this particular structure. 59 00:04:41,140 --> 00:04:42,820 How is this working. 60 00:04:43,030 --> 00:04:45,070 So that is what we call it Tupilak. 61 00:04:45,100 --> 00:04:53,980 So let's see how a tuple work is essentially a way for containing several values in all their insight 62 00:04:54,040 --> 00:04:55,600 only one invaluable. 63 00:04:55,750 --> 00:05:00,480 So I could in theory I could say larrup let's say. 64 00:05:00,520 --> 00:05:10,610 My name is going to be a Panthers of Amir and Jake for instance. 65 00:05:10,630 --> 00:05:15,920 So that's essentially a but is it is a tuple of two variables. 66 00:05:16,000 --> 00:05:17,540 Both of them are or there. 67 00:05:17,560 --> 00:05:18,430 This is zero. 68 00:05:18,430 --> 00:05:22,510 This is one and they are sitting inside the variable called my name. 69 00:05:22,510 --> 00:05:28,330 So if I go in here and say print and in the printer I say my name is zero. 70 00:05:28,480 --> 00:05:31,080 That's going to print on here I'm guessing. 71 00:05:31,450 --> 00:05:33,460 Let's see that. 72 00:05:33,460 --> 00:05:40,780 So yes I was going to print that and then I could of course go ahead and use Avorn or I could do this 73 00:05:40,900 --> 00:05:51,630 I could go ahead and actually name them I could say first name is here and then last name is J. 74 00:05:51,640 --> 00:05:58,090 And now I could not only access them by their index I could not live sustained by their names I could 75 00:05:58,090 --> 00:06:00,950 say print my name by that. 76 00:06:00,970 --> 00:06:03,970 So let's see how that works. 77 00:06:04,030 --> 00:06:05,540 And again I get the same thing. 78 00:06:05,590 --> 00:06:12,370 So tuples are a very good way for combining multiple values that for whatever reason they have to be 79 00:06:12,370 --> 00:06:16,880 together put them all inside the one variable but give them names. 80 00:06:16,960 --> 00:06:19,360 Tuples are kind of like. 81 00:06:19,440 --> 00:06:22,550 They can more concise version of struct. 82 00:06:22,720 --> 00:06:27,200 So you can kind of think of them as astroid if you already know what they saw. 83 00:06:27,210 --> 00:06:30,370 So in a programming language if not don't worry about it. 84 00:06:30,370 --> 00:06:32,910 Once you get to a structure we will learn about those. 85 00:06:32,910 --> 00:06:39,250 But you put it on much like like a concise and a brief version of a strike that they do things for us 86 00:06:39,250 --> 00:06:46,660 by giving them names we could potentially you know put this as to be multiple values for different reasons 87 00:06:46,910 --> 00:06:56,470 so they could be there are user first name last name and then I would say you ID that unique ID is going 88 00:06:56,470 --> 00:06:58,040 to be somebody else. 89 00:06:58,210 --> 00:07:07,020 And I would say their email is going to be and thats going to be an email address essentially. 90 00:07:07,030 --> 00:07:08,460 So on and so forth. 91 00:07:08,470 --> 00:07:14,900 And now if I wanted to access this guy I could simply go ahead and say user not email. 92 00:07:15,190 --> 00:07:20,350 I would print for me this particular you know value that it has in its email. 93 00:07:20,350 --> 00:07:27,640 In this case I mean are the examples that come not this is the same format we are following in a dictionary. 94 00:07:27,670 --> 00:07:34,830 We are saying give me a tuple of two values let's call them key and. 95 00:07:35,210 --> 00:07:35,530 Right. 96 00:07:35,560 --> 00:07:42,430 I'm going to say a tuple that is called Keyon Val inside my dictionary because I know every object in 97 00:07:42,430 --> 00:07:46,250 here is of that nature is a key and Yvonne. 98 00:07:46,320 --> 00:07:54,010 And now I could go ahead and say Gee and of course Saval obviously is going to work the way we expect. 99 00:07:54,040 --> 00:07:55,120 So that's about that. 100 00:07:55,120 --> 00:08:01,770 That's how we make dictionaries in swift that's how we introduce them as literature. 101 00:08:01,930 --> 00:08:08,350 If you want we could explicitly tell the compiler that they are dictionaries of the type whatever it 102 00:08:08,350 --> 00:08:10,000 is they are trying to do. 103 00:08:10,060 --> 00:08:16,960 You might sometimes in other people's code see it like this that dictionary and then they do something 104 00:08:16,960 --> 00:08:19,750 like this they write a string on a string. 105 00:08:19,990 --> 00:08:25,750 And that simply means again they are trying to note that they are trying the they are doing they are 106 00:08:25,750 --> 00:08:30,700 introducing a dictionary they are declaring a dictionary of the type of string and a string that has 107 00:08:30,910 --> 00:08:34,210 crunchie no values but it is initialized. 108 00:08:34,240 --> 00:08:39,580 This is very similar to saying that my array is empty. 109 00:08:39,610 --> 00:08:41,710 So it is initializing an array. 110 00:08:41,710 --> 00:08:43,030 There's just nothing. 111 00:08:43,060 --> 00:08:46,450 It's the same thing here the dictionary here is saying this is the type. 112 00:08:46,460 --> 00:08:48,440 And there is nothing in it. 113 00:08:48,530 --> 00:08:54,270 Of course the values could be anything that you want to imagine it could be a string an integer that 114 00:08:54,310 --> 00:09:01,120 could be a string and any that could be a string and a string depending on your situation and your case 115 00:09:01,510 --> 00:09:09,010 most likely in a dictionary the keys are a string simply because that's just what makes sense right. 116 00:09:09,040 --> 00:09:12,020 So with that in mind I think we're good. 117 00:09:12,040 --> 00:09:13,980 Let's move on to our next lesson.