1 00:00:00,600 --> 00:00:05,160 Come back again in this exercise we're going to make an array of users. 2 00:00:05,180 --> 00:00:10,240 But we are going to make sure that our users are objects of a user's class. 3 00:00:10,440 --> 00:00:16,620 Essentially we're going to bring everything we learn so far into one place to produce an app that we 4 00:00:16,620 --> 00:00:22,460 can actually store some information about different users and we can also search between them. 5 00:00:22,710 --> 00:00:28,840 So the goal of the exercise is for you to be able to write an array of users that each user has a name 6 00:00:28,870 --> 00:00:35,220 a last name a height an icon an activity an active state and then you should be able to search for a 7 00:00:35,220 --> 00:00:40,890 specific user's let's say the last search that you want to do is this you want to search for a user's 8 00:00:40,890 --> 00:00:41,560 name. 9 00:00:41,670 --> 00:00:45,970 And if that user is within your database you want to get back their height. 10 00:00:46,000 --> 00:00:51,030 You know what is the height of a user called Adam or anything to that effect. 11 00:00:51,090 --> 00:00:56,220 So I give you a little time you get a certain on your own or you can pause the video at this point and 12 00:00:56,310 --> 00:00:59,060 once you're done you can have a look at my code as well. 13 00:01:02,260 --> 00:01:06,440 OK now I'm going to go into school and I'm going to get started with this project. 14 00:01:06,550 --> 00:01:16,660 I'm going to start a new project command line tools array of users and this is going to be in strafed 15 00:01:16,980 --> 00:01:19,610 array of those swift. 16 00:01:19,770 --> 00:01:26,320 And now they have that I'm really going to start with what we need to do. 17 00:01:26,340 --> 00:01:31,530 I'm going to write this a little different than what I would ideally do simply because I want to make 18 00:01:31,530 --> 00:01:35,370 sure that you understand the process in a real life exercise. 19 00:01:35,460 --> 00:01:41,220 You would immediately start adding new files in here but I'm not going to do that just yet. 20 00:01:41,220 --> 00:01:49,830 So I'm going to say I have a class called user doesn't have a superclass simply because our Strief doesn't 21 00:01:49,830 --> 00:01:51,350 have to have a superclass. 22 00:01:51,570 --> 00:02:00,540 And the information we need our name last name height eye color and active so last name a 23 00:02:03,630 --> 00:02:09,350 last name like that a string. 24 00:02:09,360 --> 00:02:12,600 I don't know why I do that. 25 00:02:13,330 --> 00:02:23,840 Hi Karlov and ask Steve which is a billion. 26 00:02:24,060 --> 00:02:24,830 Perfect. 27 00:02:24,900 --> 00:02:27,370 I know I need to have an initialiser. 28 00:02:27,420 --> 00:02:30,070 Some got a say in it and in my in it. 29 00:02:30,070 --> 00:02:36,060 I'm going to say here's something I want to show you which I don't particularly like to use but if you 30 00:02:36,060 --> 00:02:44,340 want it you could actually stick to the same naming convention so you could say name is Thring and last 31 00:02:44,340 --> 00:02:57,020 name the string and height which is again as an integer i Horler which is going to be a string and octave 32 00:02:57,060 --> 00:03:01,840 which is going to be active which is going to be a boolean value. 33 00:03:02,100 --> 00:03:09,390 Now two things to remember that usually when I call it like this I stacked my my arguments like this. 34 00:03:09,480 --> 00:03:15,720 So it's easier for me to see them and then since I'm using the same names just so that I'm not confused 35 00:03:15,750 --> 00:03:19,370 I'm going to say self that name is name. 36 00:03:19,470 --> 00:03:22,760 That's the name that you have received as an argument. 37 00:03:22,860 --> 00:03:28,010 I'm going to copy this five times and say self-taught last name is last name. 38 00:03:28,170 --> 00:03:30,880 Self-taught hide's is height self-taught. 39 00:03:30,900 --> 00:03:36,160 Eye color is eye color and so forth active is active. 40 00:03:36,420 --> 00:03:40,260 And since this is always you know you don't have to turn to self. 41 00:03:40,260 --> 00:03:44,940 Self is automatically being initialized and is being returned using this. 42 00:03:44,970 --> 00:03:48,800 Now this is of course if you choose to use exactly the same names. 43 00:03:49,050 --> 00:03:54,960 Like I said I don't particularly like that so I usually do something like this saying input's name so 44 00:03:54,960 --> 00:04:00,330 I know that's a name that has been entered and this is like input last name. 45 00:04:00,330 --> 00:04:03,390 So that's the last name that has been entered. 46 00:04:03,430 --> 00:04:08,120 Input Hyatt's That's the input value for the hide him. 47 00:04:08,370 --> 00:04:11,680 I color again eye color. 48 00:04:11,700 --> 00:04:17,110 And finally Im active you could choose to do whichever way that you want. 49 00:04:17,100 --> 00:04:22,860 One of the goals of this course is that you craft your own way of things so which other do you think 50 00:04:22,890 --> 00:04:24,040 is going to work for you. 51 00:04:24,280 --> 00:04:26,670 No they have the class user. 52 00:04:26,670 --> 00:04:34,060 I'm actually going to go in here what this entire class say command in or file new file. 53 00:04:34,560 --> 00:04:40,830 An odd new Swift class called user. 54 00:04:41,720 --> 00:04:50,640 And in my user class I have the foundation and this is my class so I have a new file for the class user. 55 00:04:50,790 --> 00:04:57,630 So this way I make sure that I modularize my code as much as I can then go back to the main. 56 00:04:57,630 --> 00:05:03,180 So in the main Now I can actually go ahead and add the new Our users are saying actually I could go 57 00:05:03,180 --> 00:05:12,160 ahead and say wow or let's call this one user of one is going to be users. 58 00:05:12,540 --> 00:05:14,640 And here are the information I have to enter. 59 00:05:14,640 --> 00:05:15,740 I'm going to say wow 60 00:05:19,740 --> 00:05:31,090 name is your last name is Jay height his hundred eighty eye color is pink. 61 00:05:31,110 --> 00:05:35,850 Why not some random thing and then active is true. 62 00:05:35,850 --> 00:05:41,820 So I've got one use added and since I'm not going to change this user I'm actually going to convert 63 00:05:41,820 --> 00:05:44,780 that into a lot to be a static value. 64 00:05:45,200 --> 00:05:54,430 Copy this and then I added two more times so the second time under the second one is saara let's say 65 00:05:54,770 --> 00:05:55,850 Alison. 66 00:05:56,250 --> 00:06:07,260 And the third one is Steve Adams has got a hundred sixty eight and he's got a hundred ninety is purple 67 00:06:07,260 --> 00:06:10,440 color for there are eyes on green. 68 00:06:10,500 --> 00:06:19,430 I'm one of them is actually not active So so far I've got three users made of the same class type use. 69 00:06:19,440 --> 00:06:27,660 And since you know this is free if I don't have to import our users simply because user is a member 70 00:06:27,660 --> 00:06:34,080 of the same target and he's already added that's here you see target's membership. 71 00:06:34,080 --> 00:06:36,110 It is a member in here. 72 00:06:36,630 --> 00:06:42,270 Now if you remember I said one of the goals of this exercise is to try and modularize everything as 73 00:06:42,270 --> 00:06:43,510 much as I can. 74 00:06:43,530 --> 00:06:49,740 So in order to do that I'm actually going to put everything into a function and that function I'm going 75 00:06:49,740 --> 00:06:52,040 to put it in a separate file. 76 00:06:52,290 --> 00:06:55,560 So to do that I'm going to say there is a function. 77 00:06:55,560 --> 00:06:59,510 This function is called populate to users. 78 00:06:59,840 --> 00:07:04,690 I'm actually going to return an array of users for me. 79 00:07:04,710 --> 00:07:10,560 So when we call this function it produces an array of users. 80 00:07:10,560 --> 00:07:11,820 How does it do that. 81 00:07:11,880 --> 00:07:13,190 It does it like this. 82 00:07:13,210 --> 00:07:15,530 Let me read in and everything. 83 00:07:15,570 --> 00:07:17,090 So the way it does it is this. 84 00:07:17,100 --> 00:07:27,570 It says Well there is a variable called the user's array which is going to be an array of users and 85 00:07:27,800 --> 00:07:31,630 it is initialized when you make each user. 86 00:07:31,980 --> 00:07:40,240 You say users array dot appends the dots at pens. 87 00:07:40,380 --> 00:07:41,840 And what what happened to it. 88 00:07:41,880 --> 00:07:45,940 I want to append the user one for instance. 89 00:07:49,140 --> 00:07:50,330 What is that. 90 00:07:50,400 --> 00:07:52,470 It's not the user's user. 91 00:07:52,470 --> 00:07:53,180 My bad. 92 00:07:53,490 --> 00:07:56,430 So I'm going to say use two. 93 00:07:57,210 --> 00:07:58,980 And of course use three. 94 00:07:59,130 --> 00:08:02,920 So I've got to use users being added in this function. 95 00:08:03,120 --> 00:08:06,390 And each of them are added to the user's array. 96 00:08:06,390 --> 00:08:12,680 And at the end of this function I'm going to say return users array. 97 00:08:12,900 --> 00:08:20,640 So whenever this function is called It will pull views so many users I will add them to my array. 98 00:08:20,790 --> 00:08:26,900 Let me quickly re write these so it's easier to see everything in one screen. 99 00:08:27,480 --> 00:08:29,220 So that's my function. 100 00:08:29,220 --> 00:08:30,110 It populates. 101 00:08:30,160 --> 00:08:32,560 And I'm still inside my main. 102 00:08:32,700 --> 00:08:38,160 So I'm actually going to put this inside the separate file and when I go in head and say give me all 103 00:08:38,160 --> 00:08:45,360 of this code that command and I'm going to call this populates. 104 00:08:45,510 --> 00:08:47,260 So they're going to be here. 105 00:08:47,280 --> 00:08:52,750 So there's a function called populate and that is a member of the same membership. 106 00:08:53,010 --> 00:09:01,530 So up until now I have the use of a class that is like that I have my populate a function that returns 107 00:09:01,530 --> 00:09:03,880 an array and I have my main. 108 00:09:03,930 --> 00:09:09,510 So the next thing we had to do was to be able to search within that I'm going to say let's go ahead 109 00:09:09,510 --> 00:09:17,720 and do this for user name and we're fine then right. 110 00:09:17,820 --> 00:09:19,450 That's the goal in here. 111 00:09:19,470 --> 00:09:20,810 So there are two ways I could do it. 112 00:09:20,820 --> 00:09:25,260 I could actually store our array inside a separate array here. 113 00:09:25,260 --> 00:09:33,510 Let's call it this array is populate the users essentially goes ahead and makes this array and put it 114 00:09:33,510 --> 00:09:34,780 inside this array. 115 00:09:34,980 --> 00:09:37,470 Or I could directly use this guy. 116 00:09:37,530 --> 00:09:46,560 I'm going to opt out for the second solution so I'm going to save for let's say any user of the type 117 00:09:46,790 --> 00:09:51,430 user even populates users array. 118 00:09:51,600 --> 00:09:52,670 So what does it mean. 119 00:09:52,680 --> 00:09:55,220 It says Go run this function. 120 00:09:55,240 --> 00:09:56,750 I know the result of it. 121 00:09:56,790 --> 00:10:05,670 The return of it is going to be an array of users which is essentially this line which is that one I'm 122 00:10:05,670 --> 00:10:15,570 going to say for any use in that category if any user name was. 123 00:10:15,710 --> 00:10:16,970 Well that my looking for. 124 00:10:16,980 --> 00:10:18,400 I'm looking for a mirror. 125 00:10:19,230 --> 00:10:22,410 If that was the case here is what you should do. 126 00:10:22,440 --> 00:10:33,690 You should say print a mare's nest height is what is his height his height is whatever you find in any 127 00:10:33,690 --> 00:10:36,420 user that type. 128 00:10:36,960 --> 00:10:38,840 So that's about that. 129 00:10:38,850 --> 00:10:43,830 That's how we get to do our search based on these teakwood our name. 130 00:10:43,860 --> 00:10:46,080 So let's go ahead and run this quote. 131 00:10:52,990 --> 00:10:58,250 And now it says as Heidi is one hundred and eight I should have left a little space in here. 132 00:10:58,270 --> 00:11:01,350 We could also put this whole thing inside a function. 133 00:11:01,510 --> 00:11:10,360 I could say function and that function is going to be search for a name a string and then it doesn't 134 00:11:10,450 --> 00:11:14,440 return or anything it just does this whole thing for us. 135 00:11:15,960 --> 00:11:24,720 So in the function I'm going to say search for name and then it's going to say for any user a string. 136 00:11:24,710 --> 00:11:30,300 I don't know why I constantly write that is strange and so I'm going to say for any use in blah blah 137 00:11:30,300 --> 00:11:38,720 blah if any user name is let's call this one search names so it's easier to understand if it is search 138 00:11:38,720 --> 00:11:40,070 name. 139 00:11:40,440 --> 00:11:44,710 Then it says search names. 140 00:11:44,810 --> 00:11:48,750 Hi it is that users Block. 141 00:11:49,080 --> 00:11:55,460 And then I could go ahead and say search for let's say this user. 142 00:11:55,710 --> 00:12:03,420 And if we print this now or if we compile and run this project now it says Amirs height is Waddler if 143 00:12:03,420 --> 00:12:09,240 this name was signed or I think we had a cyro right. 144 00:12:09,240 --> 00:12:12,770 Sounds like he's and ninety So that's about that. 145 00:12:12,780 --> 00:12:20,580 We managed to put our class inside a separate file with some valuables and attributes in it. 146 00:12:20,640 --> 00:12:28,420 We used all of those to populate an array and return it from a a function essentially and then we use 147 00:12:28,420 --> 00:12:30,260 that in our main. 148 00:12:30,390 --> 00:12:35,680 One thing that you probably have noticed if you are perceptive enough is this. 149 00:12:35,730 --> 00:12:41,010 Imagine I had a thousand users and the very first one was the server. 150 00:12:41,310 --> 00:12:45,750 Then in that very first iteration I would I found sorrow. 151 00:12:45,960 --> 00:12:52,740 And then from there are essentially basting my processing power because I'm going to run other 999 people 152 00:12:53,100 --> 00:12:56,060 even though I have already found the person I'm looking for. 153 00:12:56,220 --> 00:12:59,990 So in this statement I'm going to say break. 154 00:13:00,500 --> 00:13:07,590 So if we make sure that if we go through this entire operation whenever we find whether we want that 155 00:13:07,590 --> 00:13:12,660 for loop breaks so we don't come back here and we don't continue it anymore. 156 00:13:13,230 --> 00:13:14,190 That's about that. 157 00:13:14,190 --> 00:13:20,230 Another thing you could do is possible that my user does this says Saraa right. 158 00:13:20,290 --> 00:13:21,450 Then they make a mistake. 159 00:13:21,450 --> 00:13:26,710 They are not looking for a capital as you know titel case and nothing happens. 160 00:13:26,710 --> 00:13:33,750 So in that case I'm going to say you know what let's compare the lower case version of username with 161 00:13:33,750 --> 00:13:40,740 the lower case version of my search item because there are lot of cases of both of them are going to 162 00:13:40,740 --> 00:13:41,760 be identical. 163 00:13:41,820 --> 00:13:42,250 Right. 164 00:13:42,360 --> 00:13:47,970 So right now the lower case of some of the lurkers if anything. 165 00:13:47,970 --> 00:13:53,090 Now it says Sauers Hyde is that which is exactly what we would want. 166 00:13:53,280 --> 00:13:57,050 So let's keep it up to here and I'll move on to our next lesson.