This lesson is an exercise to show a random e-mail depending on a touch of a button.
I was of course using swithe.
So for this lesson I've actually provided you with some photos that you know this the same set of photos
that I've been using through and through this entire application.
Of course you could Art Of course your own photos if you wanted to or if not you can just stick to my
photos.
Let's go ahead to X code and what is this project I'm going to start a new project called random photo.
So in the random photo here is what you want to see if we want to see that you have a button up here.
And that then says Ah.
Let me just get.
Better background color so I can see it myself.
Show random image.
And then there is an image of you and this image of you is somebody in here.
Right.
And then every time that we stop on this one it should show us of one of those pictures.
One of these photos that I'm going to add to my project.
So you have to do everything in swift.
Now here's the catch.
I don't want to see a repetitive picture.
It means if you tap the first time and you saw the picture of the birds the next time you top birds
should be able to be shown.
So the next time we should show one of the other four objects of our photos and then so on and so on
and so on until you finish the array.
I already gave it away.
It's an array.
And then you have to restart everything.
So at any given point to similar photos shouldn't be shown back to back.
So good luck with that.
Try telling your audience with.
I'm going to do it as well if you want to compose the video at this point and then come back to it and
see my solution as well.
So let's get started.
Here is how I'm going to do it.
First thing is I'm going to go to my view.
I know I want to content's more to be aspect Phil and I when I clip it to the boundaries I want to go
to my assistant editor get myself a little bit more face up here don't worry I will clean this up later.
I have one function called show around on action and it's called the type action the outlet.
And I also have this image of you so I'm going to call it my homage.
That's about that.
That's all I have.
And then I'm going to go to my editor and the editor I'm going to you know make things nice and tidy
the way I usually like to make them.
And because this function is happening much later on I'm going up with at the bottom.
So here are the things that you have to do.
We have to first of all put our images inside an array.
So first of all I'm going to go up here say let images array which is of type array and of the type
of what you are images.
So you I imagine it's going to be a brand new array or if you want just make it like that.
Either way you initialize your array or if you want you can actually place your objects in there.
So I'm going to actually go ahead and put my objects in there.
I'm going to say one of my objects is called What is it called Barretts GBG.
But this is not a new image.
This is actually a string.
What I'm looking for the UI image.
So I'm what I have to say.
Well you I image the name.
And here is the name that I want very similar to you I image you made named of of and were using Objective-C
now is giving me an arrow.
And this is the first time we're seeing this kind of error and that error is because of the optional
binding he's saying.
Are you sure this object exists and I'm going to say absolutely.
And if you want we can actually go to.
Let me show you the error.
The actual content of it always actually giving us two options one has to choose a default value or
the other by saying force on rapid so that you know that thing you know exists and if it is an optional
value that contains nil it's gonna abort the execution is going to fail.
But I say fix it like that.
So odd that they are so boring that one line down and then I need to repeat this process several times
right.
So one two three four.
How many are there five.
I think that's about that.
So one of them is called birds.
The other one is called Chad the JP G.
The other one is called the dog or EMG that GBG.
The third one is called 12:56 is called Happy.
JP G.
And the last one a plant is called The Road image dot pitch perfect.
So I've got all of my objects all of my photos inside an array of type II image which is exactly what
we would need for a situation like this.
So the next thing is this every time we stop in here we should get one and a random one.
So let's first get at least one around and I'm going to say rather or less because we're not going to
change that around index is going to be Orrick for random and arke Fernando is a unsigned integer 32.
So I have to actually get an integer integer like for random and then I'm going to get that modulus
two images array dot count.
So essentially whatever number we have in here and we get that modulus this which is 5 we get any number
between 0 2 4 0 1 2 3 and 4.
Perfect.
Once we got that here's what we're going to do.
We're going to say my image of you that image becomes EMG array Rand index.
So every time we press this random action bigger than your random location we show a new random image
from the images array inside the image of our image.
Let's see the once it builds launches hopefully here one two three four five six seven sometimes it
is repeating itself.
That's something we haven't you know are taking care of now.
So what can we do today to remedy that situation.
Here is my suggestion if in here in our randomisation if we got one if this random number was wrong
vine and we used up of one and used up 1 let's remove it.
So every time that we use an image you remove that from the array so it not even there to be reconsidered.
So that's not really very simple.
I'm going to say you merge array dots remover at which one do you want to remove.
I want to remove the random index once I use that.
This is now going to work but somehow it is going to actually.
What record is going to crash or application.
I will tell you why.
We also have another issue which is actually a good thing that happened is telling me you cannot change
a member of this immutable array simply because it's a let's say you become Baff.
So you become a valuable so you can be changed later on.
So based on that let's go ahead and rerun this.
And like I said this is going to work but somehow it's going to crash now soon you will see why.
First time is purrfect chat dog of birds Araud and nothing so far has been similar.
And now boom is going to crash simply because the last time that we did this the last time that we saw
whatever image we removed the very last image from our images array is not there anymore to be wrapped
around again to be repeated again.
So the solution to that is we don't touch our actual images array.
We make another 3 majors are eight that we copy from here whenever we need.
So let's see how we do that.
I'm going to go up here say this image array is the one of the cheap.
Don't touch it and since to be not going to touch it.
I'm going to make it.
Let's again then I'm going to make another e-mail just a recall value valuable current images.
Ari these are the ones we are going to change and they're going to be an array of you image you image
and they're going to be empty at the very very beginning.
So here is how we are going to deal with this entire situation.
Every time we go to shore random action I'm going to check if the current images are a dot count is
zero.
If we have used up every one in it if they are zero then current images array will become images array.
So if you have used up every one read copy everything back into it.
This obviously is going to happen the very very first time Batoche because the very very first time
current image array that count is zero.
There is nobody in it.
So then the first time the top is going to say 0 0 or so let's go copy everyone now they copy everyone.
Let's go and find a random index.
Now who should we find the random index from.
Not the images anymore.
You have to find that from the current images.
And then Image view should be read from our current images.
And we finally have to remove from current images in this situation our image array essentially works
as a backup.
It's there.
So whenever we need the copy from it it doesn't really do anything on its own is just there as a backup.
Let me try to reformat this so maybe hopefully I can show you everything in in in one screen.
I don't know if I'm going to succeed.
Just bear with me for a second and that's good.
I'm going to remove that.
We know what that is.
And then that's the other array.
I'm going to do this even though I don't like it really like that.
So they all are in front of their line.
And yes everything is now within one screen.
So we have our images array as a backup.
We have our current images array the one we use the one we use is immutable.
The one we don't want to use is immutable.
We have an image view and of course we have our real function.
We don't really need all of you not because we are not doing anything in.
So you can actually drop it all together.
And then in here in the short run I'm going to say if column image is that it doesn't have any one copy
the images are into it and then every time finder and on one plus Don't into image view.
And of course remove it from the current images Ali that see this app in action.
So once it runs and launches on my simulator I should be able to see a brand new image every time my
top.
I should mention in the interest of total accuracy that it is possible to see two images that are identical
back to bite.
That is Von's one array and that ends another copy gets generate that.
It's a rare situation but you could simply add an if statement and fix that.
So that's about that.
I would encourage you to try two other approaches such as remembering the previous index as opposed
to copying everything in one array.
What if you use only one array and remembered which one you have seen before.
So you would you know you would avoid repeating it.
There are many many ways this could be done if you had a better solution if you have an idea please
share it with the rest of us.
I see you in the next lesson.