So we're coming again in this exercise.
You're asked to do this you need to have an interface that has a button on or so it has a label.
And every time that you tap on the button in name of a city should be randomly displayed.
So every time they pop a button a city name should be chosen randomly and it should be displayed on
the screen of your iPhone or iPhone simulator.
So good luck our the we don't try to do it on your own.
And once you're done you can look at the way that I do it as well and hopefully we'll get it done.
OK.
So let's jump back into Scott in here.
I'm going to start a new project this new project I'm going to call it I have to reveal the in my top
to reveal.
I'm going to go to my main storyboard in my main storyboard.
I'm going to have a button on that bottom just like the last time I'm going to place it somewhere up
there.
For now we don't really care about the design.
There's you know where exactly we want to put objects and in here I'm going to say it's this color is
like.
And it's gone is maybe pink.
And it says how to review.
And then there is a label and this label which is here it's going to be there very similar to are actually
identical to what we did in the previous lesson.
I'm in here I'm going to go ahead and say My background is actually dark gray or maybe I don't like
green and that I'm going to go to my assistant later and I'm going to go to my age just like the last
time and remove everything.
Say this is the action.
So my boy then calls the action top to reveal the auction.
You don't really have to give such elaborate names but you know you're welcome to do it.
And I usually do it like this especially when I'm teaching but if I'm working on something for a project
I try to stick to short term names that I have a property for the label.
Let's call it resolved labor and that's about that.
So one of them is a property.
One of them is an action.
So let's go ahead to our code.
I'm going to go to the AM.
As always I'm going to clear things out.
Get rid of that.
That's my deal with the Lord.
And this is the function that they want something to happen.
So what they want to happen is I want to show a random city.
So let's go ahead and do this.
This array.
This array is called cities.
And that's going to be honest.
They out.
And in it with objects and the objects are Vancouver Sun Cisco I'm finally learning how to type that
New York may be harsh and high if that's how you spell it.
I'm going to stack them properly in here and or in here.
And of course in here nice and tidy and then I have to get one of them randomly to get one of those
cities randomly I'm gonna say.
And it's a string.
It's it the random city random city is going to be cities array objects that index for the index we
have to put a random value.
So I'm going to say integer random index is going to be all for random Let's get it modulus for which
is going to give us 0 1 2 or 3.
Or better yet let's get it modulars cities that count.
So it gives us the exact dynamic result of it.
So that's the random index and that's the object that I want and of course finally I'm going to say
result labor resolves to label dot text with with an underscore of course.
What's going on here.
There there's an hour before that.
So all on this go a text that becomes the around Sefi and that's about that.
So let's run this.
I just realized this is from New York.
I have a spell Turk sob.
You know what to.
That's going here top to reveal in San Francisco San Francisco San Francisco Toigo Vancouver.
Kind of getting all of them one at a time.
Now while this isn't working and it's all well there is a bit of a design flaw in this program and a
design flaw that we are having is this Every time I'm tapping on real action.
What's happening is it's really initialising an instance of an array called cities.
So every time I stop there is a new array being produced and that's really not a good practice simply
because your array of objects are not static like these are probably being read from some sort of a
backend or a network or some it is being provided to you.
So you shouldn't be really initializing it you have to just be using it.
So wouldn't it make more sense if we actually declare them define this array once at the beginning and
then every time that we stop we just use it.
So I'm going to go cut this whole part out and then I'm going to put this entire thing in my view that
lot which means every time the viewer loads.
Please go ahead and declare and define an array for me that is for cities which makes all the sense
it says do it just once at the very beginning.
We are however running into a brand new issue now and that issue is this.
It's called the scope of a variable dislikable N.S. our cities is being declared within the boundaries
of the body of the view of the Lord.
So anyone outside of here doesn't have access to it.
So in here the error says the use of undeclared identifier cities.
It says I don't know what it is.
How are you trying to access something that I don't even know what it is.
And that's simply because cities is what we call low call to this function only to view that lot only.
So to remedy that there are similar or different solutions.
Some of the most notable solutions are converting the cities into a property or placing them within
a private interface.
If you remember in the earlier stages of this application or any brand new application you'll have something
like this interface of your controller and and and that's essentially a private interface that holds
onto the properties that you want to use in this application.
I am however not going to use any of these.
I'm going to use a much simpler way and that is I'm going to go outside any of the methods outside the
function off of you that load or outside the function of top to reveal what I say here is the declaration
of MSRA.
So it says declare a and as array.
So there is a something called the cities and then inside the view of the Lord I'm going to get rid
of the odd declaration.
So this is the declaration and this is the definition of hate or what is in it it's being allocate and
initialized.
And here I'm using them.
So this will work.
But before we jump into actually running this application let me explain this really quickly why I wasn't
getting an error or why I was having this in year 1 declaration inside the view that load another declaration
outside because each of the declarations would work independently in their view that they would have
a called object called cities.
And then we'll have a.
Somehow you could call it a global object is not exactly global.
It is a class property but it would have a class property called cities and that cities object was this
one.
So this application would actually crash not simply because whatever is in the cities is not the same
as the one in here.
These two have nothing to do with each other because this is a brand new copy of it.
So to fix that we said let's declare it just once and get rid of these.
So if we define it once in here as of yet another thing I should mention is that while this is perfectly
valid to have the same name for two properties one of them in a different scope another way in a different
scope.
It's really not a good practice because it kind of confuses you about which one is Veach And why are
you using them in a certain way if you need a variable that must have a certain name and there must
be two copies of it which is a very rare situation.
But in that case make sure that each of the names you modified in a way that you know which one is the
local one and which one is the one that everybody can access.
So I'm going to get you out of this.
So now when are we in then everything.
And then I have my MSRA set is when the view Lote's cities gets what we call populated.
It gets all of its elements inside it.
And then when the top thing would happen let's see that in action
top to reveal that now finally before I close this lesson just a bit of a housekeeping and this isn't
really all that important but it's somehow good for us to get a sense of it.
We don't really do a lot of stuff in our view the Lord because if we do soon enough our view of the
Lord function will be cluttered with so many many different things happen.
Instead what we try to do is we try to put every bit of code inside a new function and we just call
all of those functions.
So here I'm going to say there is a function called populates aere which is going to put all of our
objects inside the array.
And this is that guy so populated area is going to be here.
And then in my view of the Lord I say that moment the view Lords do this superb You know that's a system
thing.
And then I say self populate right.
So this way I have properly organized things.
And if you want you can actually fold.
It's called that by holding the command but then clicking on it.
So if something is you know something that you do it once and then you are not going to worry about
it for a while.
You can fall dead.
Another thing you can do is you can actually cut it and put it somewhere at the bottom of the record.
So if you don't really worry about this one for a while now all we care about is doing this thing simply
because in here we have gotten to see how an array is populated and once we have the objects in it that's
all we care about from this point on we care about here.
What's going to happen then the top.
So that's about that.
We call that function and then this function gets called only when we tap on it.
This one gets called right at the beginning of the view.
So with that let's keep it up to here on we'll move on to our next lesson.