1
00:00:02,690 --> 00:00:05,410
So we're coming again in this exercise.

2
00:00:05,430 --> 00:00:12,030
You're asked to do this you need to have an interface that has a button on or so it has a label.

3
00:00:12,300 --> 00:00:18,640
And every time that you tap on the button in name of a city should be randomly displayed.

4
00:00:18,690 --> 00:00:25,470
So every time they pop a button a city name should be chosen randomly and it should be displayed on

5
00:00:25,940 --> 00:00:29,220
the screen of your iPhone or iPhone simulator.

6
00:00:29,220 --> 00:00:33,600
So good luck our the we don't try to do it on your own.

7
00:00:33,790 --> 00:00:39,510
And once you're done you can look at the way that I do it as well and hopefully we'll get it done.

8
00:00:41,810 --> 00:00:42,240
OK.

9
00:00:42,340 --> 00:00:45,430
So let's jump back into Scott in here.

10
00:00:45,460 --> 00:00:58,580
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

11
00:00:58,580 --> 00:01:01,110
to reveal.

12
00:01:01,340 --> 00:01:05,570
I'm going to go to my main storyboard in my main storyboard.

13
00:01:05,570 --> 00:01:12,440
I'm going to have a button on that bottom just like the last time I'm going to place it somewhere up

14
00:01:12,440 --> 00:01:13,760
there.

15
00:01:13,820 --> 00:01:16,470
For now we don't really care about the design.

16
00:01:16,620 --> 00:01:23,030
There's you know where exactly we want to put objects and in here I'm going to say it's this color is

17
00:01:23,620 --> 00:01:24,720
like.

18
00:01:25,140 --> 00:01:28,280
And it's gone is maybe pink.

19
00:01:28,640 --> 00:01:33,540
And it says how to review.

20
00:01:33,800 --> 00:01:43,910
And then there is a label and this label which is here it's going to be there very similar to are actually

21
00:01:43,910 --> 00:01:47,500
identical to what we did in the previous lesson.

22
00:01:47,690 --> 00:01:53,870
I'm in here I'm going to go ahead and say My background is actually dark gray or maybe I don't like

23
00:01:53,870 --> 00:02:00,920
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

24
00:02:00,920 --> 00:02:02,710
time and remove everything.

25
00:02:02,860 --> 00:02:05,600
Say this is the action.

26
00:02:05,750 --> 00:02:13,940
So my boy then calls the action top to reveal the auction.

27
00:02:13,970 --> 00:02:19,190
You don't really have to give such elaborate names but you know you're welcome to do it.

28
00:02:19,190 --> 00:02:25,490
And I usually do it like this especially when I'm teaching but if I'm working on something for a project

29
00:02:25,520 --> 00:02:31,850
I try to stick to short term names that I have a property for the label.

30
00:02:31,880 --> 00:02:36,200
Let's call it resolved labor and that's about that.

31
00:02:36,200 --> 00:02:37,610
So one of them is a property.

32
00:02:37,640 --> 00:02:39,340
One of them is an action.

33
00:02:39,470 --> 00:02:41,260
So let's go ahead to our code.

34
00:02:41,390 --> 00:02:42,930
I'm going to go to the AM.

35
00:02:43,160 --> 00:02:45,320
As always I'm going to clear things out.

36
00:02:45,320 --> 00:02:46,740
Get rid of that.

37
00:02:46,790 --> 00:02:48,850
That's my deal with the Lord.

38
00:02:48,900 --> 00:02:52,090
And this is the function that they want something to happen.

39
00:02:52,360 --> 00:02:56,390
So what they want to happen is I want to show a random city.

40
00:02:56,450 --> 00:02:58,450
So let's go ahead and do this.

41
00:02:58,650 --> 00:03:00,020
This array.

42
00:03:00,050 --> 00:03:02,430
This array is called cities.

43
00:03:02,750 --> 00:03:05,160
And that's going to be honest.

44
00:03:05,230 --> 00:03:06,780
They out.

45
00:03:07,280 --> 00:03:16,960
And in it with objects and the objects are Vancouver Sun Cisco I'm finally learning how to type that

46
00:03:17,540 --> 00:03:23,940
New York may be harsh and high if that's how you spell it.

47
00:03:24,470 --> 00:03:29,720
I'm going to stack them properly in here and or in here.

48
00:03:29,870 --> 00:03:37,580
And of course in here nice and tidy and then I have to get one of them randomly to get one of those

49
00:03:37,580 --> 00:03:39,660
cities randomly I'm gonna say.

50
00:03:39,920 --> 00:03:41,190
And it's a string.

51
00:03:41,310 --> 00:03:51,290
It's it the random city random city is going to be cities array objects that index for the index we

52
00:03:51,290 --> 00:03:53,850
have to put a random value.

53
00:03:53,870 --> 00:04:04,270
So I'm going to say integer random index is going to be all for random Let's get it modulus for which

54
00:04:04,290 --> 00:04:08,890
is going to give us 0 1 2 or 3.

55
00:04:09,020 --> 00:04:13,750
Or better yet let's get it modulars cities that count.

56
00:04:13,910 --> 00:04:17,100
So it gives us the exact dynamic result of it.

57
00:04:17,130 --> 00:04:22,580
So that's the random index and that's the object that I want and of course finally I'm going to say

58
00:04:22,580 --> 00:04:31,480
result labor resolves to label dot text with with an underscore of course.

59
00:04:33,130 --> 00:04:34,510
What's going on here.

60
00:04:34,630 --> 00:04:36,560
There there's an hour before that.

61
00:04:36,750 --> 00:04:48,270
So all on this go a text that becomes the around Sefi and that's about that.

62
00:04:48,270 --> 00:04:52,550
So let's run this.

63
00:04:52,600 --> 00:04:54,540
I just realized this is from New York.

64
00:04:54,550 --> 00:04:56,750
I have a spell Turk sob.

65
00:04:57,220 --> 00:04:58,680
You know what to.

66
00:04:59,620 --> 00:05:06,170
That's going here top to reveal in San Francisco San Francisco San Francisco Toigo Vancouver.

67
00:05:06,230 --> 00:05:08,780
Kind of getting all of them one at a time.

68
00:05:08,920 --> 00:05:16,420
Now while this isn't working and it's all well there is a bit of a design flaw in this program and a

69
00:05:16,420 --> 00:05:23,020
design flaw that we are having is this Every time I'm tapping on real action.

70
00:05:23,140 --> 00:05:29,800
What's happening is it's really initialising an instance of an array called cities.

71
00:05:29,830 --> 00:05:35,650
So every time I stop there is a new array being produced and that's really not a good practice simply

72
00:05:35,650 --> 00:05:41,860
because your array of objects are not static like these are probably being read from some sort of a

73
00:05:41,860 --> 00:05:45,900
backend or a network or some it is being provided to you.

74
00:05:46,030 --> 00:05:50,010
So you shouldn't be really initializing it you have to just be using it.

75
00:05:50,200 --> 00:05:57,820
So wouldn't it make more sense if we actually declare them define this array once at the beginning and

76
00:05:57,820 --> 00:06:01,130
then every time that we stop we just use it.

77
00:06:01,300 --> 00:06:07,770
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

78
00:06:07,780 --> 00:06:10,790
lot which means every time the viewer loads.

79
00:06:10,840 --> 00:06:18,820
Please go ahead and declare and define an array for me that is for cities which makes all the sense

80
00:06:18,820 --> 00:06:23,210
it says do it just once at the very beginning.

81
00:06:23,380 --> 00:06:27,770
We are however running into a brand new issue now and that issue is this.

82
00:06:27,940 --> 00:06:36,370
It's called the scope of a variable dislikable N.S. our cities is being declared within the boundaries

83
00:06:36,520 --> 00:06:39,120
of the body of the view of the Lord.

84
00:06:39,270 --> 00:06:43,410
So anyone outside of here doesn't have access to it.

85
00:06:43,450 --> 00:06:48,240
So in here the error says the use of undeclared identifier cities.

86
00:06:48,250 --> 00:06:50,130
It says I don't know what it is.

87
00:06:50,130 --> 00:06:53,360
How are you trying to access something that I don't even know what it is.

88
00:06:53,500 --> 00:07:01,370
And that's simply because cities is what we call low call to this function only to view that lot only.

89
00:07:01,570 --> 00:07:04,800
So to remedy that there are similar or different solutions.

90
00:07:04,810 --> 00:07:11,740
Some of the most notable solutions are converting the cities into a property or placing them within

91
00:07:11,770 --> 00:07:13,520
a private interface.

92
00:07:13,660 --> 00:07:20,200
If you remember in the earlier stages of this application or any brand new application you'll have something

93
00:07:20,200 --> 00:07:27,940
like this interface of your controller and and and that's essentially a private interface that holds

94
00:07:27,940 --> 00:07:31,720
onto the properties that you want to use in this application.

95
00:07:31,720 --> 00:07:34,820
I am however not going to use any of these.

96
00:07:34,900 --> 00:07:42,010
I'm going to use a much simpler way and that is I'm going to go outside any of the methods outside the

97
00:07:42,010 --> 00:07:49,480
function off of you that load or outside the function of top to reveal what I say here is the declaration

98
00:07:49,510 --> 00:07:50,810
of MSRA.

99
00:07:51,100 --> 00:07:56,390
So it says declare a and as array.

100
00:07:56,560 --> 00:08:03,220
So there is a something called the cities and then inside the view of the Lord I'm going to get rid

101
00:08:03,280 --> 00:08:05,810
of the odd declaration.

102
00:08:05,980 --> 00:08:11,500
So this is the declaration and this is the definition of hate or what is in it it's being allocate and

103
00:08:11,500 --> 00:08:12,500
initialized.

104
00:08:12,700 --> 00:08:14,560
And here I'm using them.

105
00:08:14,650 --> 00:08:15,950
So this will work.

106
00:08:16,090 --> 00:08:23,770
But before we jump into actually running this application let me explain this really quickly why I wasn't

107
00:08:23,770 --> 00:08:30,720
getting an error or why I was having this in year 1 declaration inside the view that load another declaration

108
00:08:30,730 --> 00:08:36,880
outside because each of the declarations would work independently in their view that they would have

109
00:08:36,970 --> 00:08:39,470
a called object called cities.

110
00:08:39,730 --> 00:08:41,260
And then we'll have a.

111
00:08:41,350 --> 00:08:44,950
Somehow you could call it a global object is not exactly global.

112
00:08:44,950 --> 00:08:52,990
It is a class property but it would have a class property called cities and that cities object was this

113
00:08:52,990 --> 00:08:53,670
one.

114
00:08:53,710 --> 00:09:00,280
So this application would actually crash not simply because whatever is in the cities is not the same

115
00:09:00,280 --> 00:09:01,580
as the one in here.

116
00:09:01,630 --> 00:09:05,930
These two have nothing to do with each other because this is a brand new copy of it.

117
00:09:06,130 --> 00:09:11,550
So to fix that we said let's declare it just once and get rid of these.

118
00:09:11,560 --> 00:09:18,010
So if we define it once in here as of yet another thing I should mention is that while this is perfectly

119
00:09:18,010 --> 00:09:24,730
valid to have the same name for two properties one of them in a different scope another way in a different

120
00:09:24,730 --> 00:09:25,230
scope.

121
00:09:25,390 --> 00:09:31,000
It's really not a good practice because it kind of confuses you about which one is Veach And why are

122
00:09:31,000 --> 00:09:37,330
you using them in a certain way if you need a variable that must have a certain name and there must

123
00:09:37,330 --> 00:09:40,100
be two copies of it which is a very rare situation.

124
00:09:40,300 --> 00:09:45,700
But in that case make sure that each of the names you modified in a way that you know which one is the

125
00:09:45,700 --> 00:09:49,050
local one and which one is the one that everybody can access.

126
00:09:49,240 --> 00:09:51,470
So I'm going to get you out of this.

127
00:09:51,590 --> 00:09:54,610
So now when are we in then everything.

128
00:09:54,880 --> 00:10:01,360
And then I have my MSRA set is when the view Lote's cities gets what we call populated.

129
00:10:01,350 --> 00:10:03,530
It gets all of its elements inside it.

130
00:10:03,760 --> 00:10:07,530
And then when the top thing would happen let's see that in action

131
00:10:10,910 --> 00:10:20,290
top to reveal that now finally before I close this lesson just a bit of a housekeeping and this isn't

132
00:10:20,290 --> 00:10:24,860
really all that important but it's somehow good for us to get a sense of it.

133
00:10:25,000 --> 00:10:31,380
We don't really do a lot of stuff in our view the Lord because if we do soon enough our view of the

134
00:10:31,390 --> 00:10:35,580
Lord function will be cluttered with so many many different things happen.

135
00:10:35,890 --> 00:10:43,630
Instead what we try to do is we try to put every bit of code inside a new function and we just call

136
00:10:43,630 --> 00:10:45,180
all of those functions.

137
00:10:45,190 --> 00:10:53,050
So here I'm going to say there is a function called populates aere which is going to put all of our

138
00:10:53,050 --> 00:10:54,650
objects inside the array.

139
00:10:54,670 --> 00:10:59,200
And this is that guy so populated area is going to be here.

140
00:10:59,560 --> 00:11:05,680
And then in my view of the Lord I say that moment the view Lords do this superb You know that's a system

141
00:11:05,680 --> 00:11:06,170
thing.

142
00:11:06,460 --> 00:11:10,450
And then I say self populate right.

143
00:11:10,840 --> 00:11:13,780
So this way I have properly organized things.

144
00:11:13,780 --> 00:11:15,550
And if you want you can actually fold.

145
00:11:15,580 --> 00:11:21,330
It's called that by holding the command but then clicking on it.

146
00:11:21,350 --> 00:11:25,630
So if something is you know something that you do it once and then you are not going to worry about

147
00:11:25,630 --> 00:11:26,500
it for a while.

148
00:11:26,600 --> 00:11:27,570
You can fall dead.

149
00:11:27,650 --> 00:11:32,450
Another thing you can do is you can actually cut it and put it somewhere at the bottom of the record.

150
00:11:32,510 --> 00:11:39,080
So if you don't really worry about this one for a while now all we care about is doing this thing simply

151
00:11:39,080 --> 00:11:47,000
because in here we have gotten to see how an array is populated and once we have the objects in it that's

152
00:11:47,000 --> 00:11:50,190
all we care about from this point on we care about here.

153
00:11:50,210 --> 00:11:52,290
What's going to happen then the top.

154
00:11:52,310 --> 00:11:54,470
So that's about that.

155
00:11:54,560 --> 00:11:59,840
We call that function and then this function gets called only when we tap on it.

156
00:11:59,840 --> 00:12:02,970
This one gets called right at the beginning of the view.

157
00:12:03,410 --> 00:12:08,060
So with that let's keep it up to here on we'll move on to our next lesson.
