Come back again in this exercise we're going to make an array of users.
But we are going to make sure that our users are objects of a user's class.
Essentially we're going to bring everything we learn so far into one place to produce an app that we
can actually store some information about different users and we can also search between them.
So the goal of the exercise is for you to be able to write an array of users that each user has a name
a last name a height an icon an activity an active state and then you should be able to search for a
specific user's let's say the last search that you want to do is this you want to search for a user's
name.
And if that user is within your database you want to get back their height.
You know what is the height of a user called Adam or anything to that effect.
So I give you a little time you get a certain on your own or you can pause the video at this point and
once you're done you can have a look at my code as well.
OK now I'm going to go into school and I'm going to get started with this project.
I'm going to start a new project command line tools array of users and this is going to be in strafed
array of those swift.
And now they have that I'm really going to start with what we need to do.
I'm going to write this a little different than what I would ideally do simply because I want to make
sure that you understand the process in a real life exercise.
You would immediately start adding new files in here but I'm not going to do that just yet.
So I'm going to say I have a class called user doesn't have a superclass simply because our Strief doesn't
have to have a superclass.
And the information we need our name last name height eye color and active so last name a
last name like that a string.
I don't know why I do that.
Hi Karlov and ask Steve which is a billion.
Perfect.
I know I need to have an initialiser.
Some got a say in it and in my in it.
I'm going to say here's something I want to show you which I don't particularly like to use but if you
want it you could actually stick to the same naming convention so you could say name is Thring and last
name the string and height which is again as an integer i Horler which is going to be a string and octave
which is going to be active which is going to be a boolean value.
Now two things to remember that usually when I call it like this I stacked my my arguments like this.
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
I'm going to say self that name is name.
That's the name that you have received as an argument.
I'm going to copy this five times and say self-taught last name is last name.
Self-taught hide's is height self-taught.
Eye color is eye color and so forth active is active.
And since this is always you know you don't have to turn to self.
Self is automatically being initialized and is being returned using this.
Now this is of course if you choose to use exactly the same names.
Like I said I don't particularly like that so I usually do something like this saying input's name so
I know that's a name that has been entered and this is like input last name.
So that's the last name that has been entered.
Input Hyatt's That's the input value for the hide him.
I color again eye color.
And finally Im active you could choose to do whichever way that you want.
One of the goals of this course is that you craft your own way of things so which other do you think
is going to work for you.
No they have the class user.
I'm actually going to go in here what this entire class say command in or file new file.
An odd new Swift class called user.
And in my user class I have the foundation and this is my class so I have a new file for the class user.
So this way I make sure that I modularize my code as much as I can then go back to the main.
So in the main Now I can actually go ahead and add the new Our users are saying actually I could go
ahead and say wow or let's call this one user of one is going to be users.
And here are the information I have to enter.
I'm going to say wow
name is your last name is Jay height his hundred eighty eye color is pink.
Why not some random thing and then active is true.
So I've got one use added and since I'm not going to change this user I'm actually going to convert
that into a lot to be a static value.
Copy this and then I added two more times so the second time under the second one is saara let's say
Alison.
And the third one is Steve Adams has got a hundred sixty eight and he's got a hundred ninety is purple
color for there are eyes on green.
I'm one of them is actually not active So so far I've got three users made of the same class type use.
And since you know this is free if I don't have to import our users simply because user is a member
of the same target and he's already added that's here you see target's membership.
It is a member in here.
Now if you remember I said one of the goals of this exercise is to try and modularize everything as
much as I can.
So in order to do that I'm actually going to put everything into a function and that function I'm going
to put it in a separate file.
So to do that I'm going to say there is a function.
This function is called populate to users.
I'm actually going to return an array of users for me.
So when we call this function it produces an array of users.
How does it do that.
It does it like this.
Let me read in and everything.
So the way it does it is this.
It says Well there is a variable called the user's array which is going to be an array of users and
it is initialized when you make each user.
You say users array dot appends the dots at pens.
And what what happened to it.
I want to append the user one for instance.
What is that.
It's not the user's user.
My bad.
So I'm going to say use two.
And of course use three.
So I've got to use users being added in this function.
And each of them are added to the user's array.
And at the end of this function I'm going to say return users array.
So whenever this function is called It will pull views so many users I will add them to my array.
Let me quickly re write these so it's easier to see everything in one screen.
So that's my function.
It populates.
And I'm still inside my main.
So I'm actually going to put this inside the separate file and when I go in head and say give me all
of this code that command and I'm going to call this populates.
So they're going to be here.
So there's a function called populate and that is a member of the same membership.
So up until now I have the use of a class that is like that I have my populate a function that returns
an array and I have my main.
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
and do this for user name and we're fine then right.
That's the goal in here.
So there are two ways I could do it.
I could actually store our array inside a separate array here.
Let's call it this array is populate the users essentially goes ahead and makes this array and put it
inside this array.
Or I could directly use this guy.
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
user even populates users array.
So what does it mean.
It says Go run this function.
I know the result of it.
The return of it is going to be an array of users which is essentially this line which is that one I'm
going to say for any use in that category if any user name was.
Well that my looking for.
I'm looking for a mirror.
If that was the case here is what you should do.
You should say print a mare's nest height is what is his height his height is whatever you find in any
user that type.
So that's about that.
That's how we get to do our search based on these teakwood our name.
So let's go ahead and run this quote.
And now it says as Heidi is one hundred and eight I should have left a little space in here.
We could also put this whole thing inside a function.
I could say function and that function is going to be search for a name a string and then it doesn't
return or anything it just does this whole thing for us.
So in the function I'm going to say search for name and then it's going to say for any user a string.
I don't know why I constantly write that is strange and so I'm going to say for any use in blah blah
blah if any user name is let's call this one search names so it's easier to understand if it is search
name.
Then it says search names.
Hi it is that users Block.
And then I could go ahead and say search for let's say this user.
And if we print this now or if we compile and run this project now it says Amirs height is Waddler if
this name was signed or I think we had a cyro right.
Sounds like he's and ninety So that's about that.
We managed to put our class inside a separate file with some valuables and attributes in it.
We used all of those to populate an array and return it from a a function essentially and then we use
that in our main.
One thing that you probably have noticed if you are perceptive enough is this.
Imagine I had a thousand users and the very first one was the server.
Then in that very first iteration I would I found sorrow.
And then from there are essentially basting my processing power because I'm going to run other 999 people
even though I have already found the person I'm looking for.
So in this statement I'm going to say break.
So if we make sure that if we go through this entire operation whenever we find whether we want that
for loop breaks so we don't come back here and we don't continue it anymore.
That's about that.
Another thing you could do is possible that my user does this says Saraa right.
Then they make a mistake.
They are not looking for a capital as you know titel case and nothing happens.
So in that case I'm going to say you know what let's compare the lower case version of username with
the lower case version of my search item because there are lot of cases of both of them are going to
be identical.
Right.
So right now the lower case of some of the lurkers if anything.
Now it says Sauers Hyde is that which is exactly what we would want.
So let's keep it up to here and I'll move on to our next lesson.