1
00:00:05,650 --> 00:00:06,770
Know what's up everyone.

2
00:00:06,770 --> 00:00:12,840
Mark right here at the slopes dot com and we've done some really cool stuff in our apps so far we've

3
00:00:12,840 --> 00:00:14,940
gotten the user interface built.

4
00:00:14,940 --> 00:00:18,310
We've got Segway so you can go back and forth between screens even now.

5
00:00:18,330 --> 00:00:23,430
Got a model built and I re-actions tied it to our league Visi and we don't let you go onto the next

6
00:00:23,430 --> 00:00:26,040
screen unless you've selected an option which is really cool.

7
00:00:26,040 --> 00:00:29,040
And then of course is stored in the player model.

8
00:00:29,040 --> 00:00:30,330
It's a struct.

9
00:00:30,480 --> 00:00:36,960
And so the next thing and possibly last thing we need to do here is actually pass data between View

10
00:00:36,960 --> 00:00:37,590
controllers.

11
00:00:37,590 --> 00:00:42,390
Now this is a topic that a lot of people struggle with and then they've done some really weird things

12
00:00:42,600 --> 00:00:47,280
to get data talking between View controllers going to make it really easy for you.

13
00:00:47,310 --> 00:00:47,740
OK.

14
00:00:47,760 --> 00:00:52,800
And the function that we're looking for here is already built in your view controller whenever you create

15
00:00:52,800 --> 00:00:56,180
a new one and it's called prepare for Segway.

16
00:00:56,360 --> 00:00:57,070
OK.

17
00:00:57,150 --> 00:00:59,070
And when you read a function by the way.

18
00:00:59,240 --> 00:00:59,750
OK.

19
00:00:59,770 --> 00:01:01,900
When you the actual function name.

20
00:01:01,950 --> 00:01:02,310
All right.

21
00:01:02,370 --> 00:01:03,390
It's going to look like this.

22
00:01:03,390 --> 00:01:05,760
Prepare for Segway.

23
00:01:05,760 --> 00:01:11,670
So this right here is the name of a parameter and this is the variable name that the parameter is going

24
00:01:11,670 --> 00:01:12,470
to take on.

25
00:01:12,750 --> 00:01:19,220
And so this this and this all kind of compose of the function definition of the function name.

26
00:01:19,290 --> 00:01:24,160
And if you have other subsequent parameters those will also be added to the name of the function and

27
00:01:24,180 --> 00:01:25,590
to show you what I mean.

28
00:01:25,920 --> 00:01:31,250
So this is right here all nice ready for us but if I type in the word prepare OK.

29
00:01:31,350 --> 00:01:35,570
Notice how it says prepare for Segway interface builder sender it's all right there.

30
00:01:36,760 --> 00:01:37,270
OK.

31
00:01:37,340 --> 00:01:38,680
So that's the function definition.

32
00:01:38,690 --> 00:01:39,690
OK.

33
00:01:39,710 --> 00:01:46,130
And it's an override function meaning the default view controller that you're inheriting from We'll

34
00:01:46,190 --> 00:01:50,960
call this and we don't need to call super on this or anything like that because we're overwriting it.

35
00:01:51,230 --> 00:01:54,260
Typically what that means is when you're writing and you don't call super.

36
00:01:54,260 --> 00:01:59,050
It means that you don't need the view controller implementation of it you don't need that code to run.

37
00:01:59,090 --> 00:02:01,550
Our code is going to take over from here.

38
00:02:01,550 --> 00:02:03,900
Otherwise super would call that parent code.

39
00:02:04,070 --> 00:02:10,390
OK so what we want to do is we want to see which side we were going to again remember what I said.

40
00:02:10,430 --> 00:02:15,350
I said if you have multiple segues that can be triggered from a single view controller you need to check

41
00:02:15,350 --> 00:02:16,240
which one is which.

42
00:02:16,250 --> 00:02:18,540
And you might pass different data to different segues.

43
00:02:18,560 --> 00:02:19,340
OK.

44
00:02:19,340 --> 00:02:22,820
In our case we only have one so it makes it really nice but we're still going to have to write the same

45
00:02:22,820 --> 00:02:25,470
code we would write if there were multiple segues.

46
00:02:25,610 --> 00:02:27,470
And this is the code.

47
00:02:27,470 --> 00:02:34,640
So if let And let's just say skill VC equals segue.

48
00:02:34,650 --> 00:02:38,710
Destination has skewl.

49
00:02:38,800 --> 00:02:41,070
VC All right.

50
00:02:41,110 --> 00:02:42,560
Let me let me break it down.

51
00:02:42,650 --> 00:02:42,960
OK.

52
00:02:42,960 --> 00:02:49,540
So what we're saying here is we're doing and if let's sequence and when and if sequence is OK.

53
00:02:49,680 --> 00:02:52,790
Basically it will try and run an operation.

54
00:02:52,980 --> 00:02:58,380
OK it'll try and run the statement in this case it's going to try and assign the destination view controller

55
00:02:58,410 --> 00:03:01,000
into this variable or this constant.

56
00:03:01,000 --> 00:03:02,060
The lead is a constant.

57
00:03:02,150 --> 00:03:02,390
OK.

58
00:03:02,400 --> 00:03:06,290
So it's going to try and do this if it succeeds it will execute inside the code block.

59
00:03:06,660 --> 00:03:09,780
If it fails then this statement will fail.

60
00:03:10,140 --> 00:03:10,490
OK.

61
00:03:10,510 --> 00:03:19,340
So if segue that destination so were the failure could happen is right here is as gate as with the question

62
00:03:19,340 --> 00:03:21,990
mark is saying let's grab the Segway destination.

63
00:03:22,070 --> 00:03:28,570
Well if you look at here at this destination by command click it destination is a type you view controller

64
00:03:28,590 --> 00:03:31,360
so it's just a generic view controller.

65
00:03:31,360 --> 00:03:36,850
But our destination that we want to go to is the skill of you know skilled VC does inherit from you

66
00:03:36,870 --> 00:03:37,800
view controller.

67
00:03:38,140 --> 00:03:46,540
And so with object oriented programming we can downcast a skilled VC or rather a destination view controller

68
00:03:46,540 --> 00:03:49,050
as a skill AVC because it does inherent.

69
00:03:49,060 --> 00:03:53,320
But let's say you skill AVC did not inherit from your controller it was just her own class.

70
00:03:53,320 --> 00:03:56,410
Then this would fill but it won't in this case.

71
00:03:56,500 --> 00:04:00,940
So what we're saying here is let's grab the destination that we're going to go to so it checks the Segway

72
00:04:01,930 --> 00:04:03,570
for the destination view controller.

73
00:04:03,580 --> 00:04:05,500
So the skill Visi in this case.

74
00:04:05,680 --> 00:04:06,340
OK.

75
00:04:06,610 --> 00:04:08,990
And then it's going to try and grab it.

76
00:04:09,010 --> 00:04:14,200
So it's going to grab the view controller code that exists what's in our storyboards So this file right

77
00:04:14,200 --> 00:04:18,420
here and OK it's going to load it in memory it's going to try and it's going to grab it.

78
00:04:18,700 --> 00:04:24,010
And of course it'll work because that's what's designed to work and it'll grab this code right here

79
00:04:24,100 --> 00:04:28,210
and it's going to send it back to you and store it here in this variable.

80
00:04:28,560 --> 00:04:29,310
OK.

81
00:04:29,380 --> 00:04:35,260
And the prepare for Segway happens whenever a Segway is triggered this prepare function is going to

82
00:04:35,260 --> 00:04:39,860
be called so it lets you do things before the Segway actually loads on the screen.

83
00:04:40,120 --> 00:04:42,940
And this is where you will pass data between View controllers.

84
00:04:42,940 --> 00:04:47,760
So again what we're doing here is we're saying it's the skill we see.

85
00:04:47,770 --> 00:04:52,180
So if we can grab it successfully then we're going to do something again and we had multiple controllers

86
00:04:52,180 --> 00:04:56,540
that you could choose from let's say there are five different screens you might go to based on the selection.

87
00:04:56,770 --> 00:05:00,080
Then what we would do is we would have like if let you know.

88
00:05:00,100 --> 00:05:05,610
And then you know else if let And then we would say you know other VC.

89
00:05:05,610 --> 00:05:09,400
And then we would cast a different Visi and then it would go down until it finds the proper one.

90
00:05:09,400 --> 00:05:10,560
And then we send the data.

91
00:05:10,680 --> 00:05:11,410
OK.

92
00:05:11,740 --> 00:05:15,040
So we're grabbing the next few controller we want to go to.

93
00:05:15,130 --> 00:05:17,000
And if this is confusing to you it's OK.

94
00:05:17,050 --> 00:05:18,880
You're just learning this takes time.

95
00:05:18,880 --> 00:05:22,720
You're going to do this many times over and you can always watch this video again.

96
00:05:22,750 --> 00:05:22,930
OK.

97
00:05:22,930 --> 00:05:26,020
So grab the skill Visi and what we want to do.

98
00:05:26,020 --> 00:05:27,800
We want to pass data into it.

99
00:05:27,850 --> 00:05:35,010
So we're going to need to do is store a player variable in our skewl VC and we don't have one yet so

100
00:05:35,020 --> 00:05:36,050
let's do that now.

101
00:05:36,340 --> 00:05:45,200
So over here let's go ahead and say var player type player and let's do it in Italy unwrapped optional.

102
00:05:45,220 --> 00:05:51,100
What that means is no matter what we promise we promise that there's going to be code in there that

103
00:05:51,100 --> 00:05:52,270
it's not going to be nil.

104
00:05:52,380 --> 00:05:53,290
OK.

105
00:05:53,350 --> 00:05:55,080
And so that's what we're promising.

106
00:05:55,090 --> 00:06:02,410
And so what we're going to do is we're going to go over here and we're going to say skill Visi top player

107
00:06:02,530 --> 00:06:07,830
the one that we just created OK and did not save the file.

108
00:06:14,180 --> 00:06:18,690
Skill we up player there we go.

109
00:06:18,690 --> 00:06:19,400
Now it's popping up.

110
00:06:19,440 --> 00:06:19,910
OK.

111
00:06:20,070 --> 00:06:23,750
So skill obviously the player equals player.

112
00:06:24,090 --> 00:06:25,510
You're like what's going on here.

113
00:06:25,530 --> 00:06:27,310
These names of the same.

114
00:06:27,330 --> 00:06:31,260
Well we are referring to the player that we saved and this view controller here.

115
00:06:31,430 --> 00:06:31,760
OK.

116
00:06:31,780 --> 00:06:33,660
This is here in league visi.

117
00:06:33,690 --> 00:06:35,630
We've set the player here.

118
00:06:36,210 --> 00:06:41,460
And what we're doing is we are just passing this player from the screen onto the player on the screen.

119
00:06:41,460 --> 00:06:45,690
So literally this is where the handoff comes you know like footballs being handed off UK.

120
00:06:45,810 --> 00:06:51,210
Same exact thing we're handing off the data from the player on the league Visi and we're storing it

121
00:06:51,300 --> 00:06:53,750
in the player in the school of visi.

122
00:06:53,940 --> 00:07:01,740
Now something really important to understand when you are in prepare for segue this will always always

123
00:07:02,220 --> 00:07:09,600
always always be called before you did lotas called on the destination view controller.

124
00:07:09,600 --> 00:07:19,230
So remember this very important prepare for Segway is always called before you did load on the destination

125
00:07:19,230 --> 00:07:19,810
view controller.

126
00:07:19,830 --> 00:07:26,700
So what you can do is you can always guarantee that data will be available in view to load if you initialized

127
00:07:26,700 --> 00:07:26,790
it.

128
00:07:26,790 --> 00:07:35,680
So if I was to do something like this player datt desired League.

129
00:07:36,640 --> 00:07:40,650
OK if I was to do that it will not be nil.

130
00:07:40,750 --> 00:07:42,200
It can't be OK.

131
00:07:42,550 --> 00:07:46,330
And it says expression implicitly coerced from string.

132
00:07:46,330 --> 00:07:53,800
Now here in our player model it's because we have them as optionals And you know probably here we should

133
00:07:53,800 --> 00:07:58,330
have these as implicitly unwrap the optionals as well too because we have to have data in order for

134
00:07:58,910 --> 00:07:59,980
this to happen.

135
00:08:00,070 --> 00:08:03,020
For this app to run and this can be very confusing OK.

136
00:08:03,290 --> 00:08:07,830
Do I make it optional do I make it implicitly unwrapped optional so remember this.

137
00:08:07,870 --> 00:08:09,990
OK here is the rule.

138
00:08:10,510 --> 00:08:17,650
If you can promise that data is going to be there mean your app must have data in order to run.

139
00:08:17,740 --> 00:08:20,950
Then you can make it an implicitly unwrapped optional.

140
00:08:20,950 --> 00:08:24,970
If you don't know whether data is going to be there or not and that's part of your design.

141
00:08:25,180 --> 00:08:27,580
OK it must be an optional.

142
00:08:27,700 --> 00:08:32,650
OK so if you are promised data is going to be there because your app can't run without it you can assume

143
00:08:32,650 --> 00:08:35,230
it's going to be there and make it implicitly unwrapped.

144
00:08:35,290 --> 00:08:39,010
If there was no data in here and you tried to access it yes it would crash but you're promising that

145
00:08:39,010 --> 00:08:43,630
there's going to be data there whereas the optional saying well might application design means that

146
00:08:43,630 --> 00:08:44,350
there might not be data.

147
00:08:44,350 --> 00:08:46,180
Maybe it's optional field who knows.

148
00:08:46,180 --> 00:08:47,840
So that's the rule.

149
00:08:47,890 --> 00:08:49,360
And again it could be confusing.

150
00:08:49,360 --> 00:08:51,350
It takes lots of practice but you'll get it.

151
00:08:51,680 --> 00:08:57,710
So this morning should go away and there you go.

152
00:08:57,720 --> 00:09:01,350
And so what we're doing is we're going to print this here so let's just go ahead and run our app and

153
00:09:01,350 --> 00:09:03,400
see if it actually prints as expected.

154
00:09:04,210 --> 00:09:08,520
So what should happen is we're going to select the desired League.

155
00:09:08,560 --> 00:09:08,990
OK.

156
00:09:09,040 --> 00:09:12,520
We're going to go to the next screen and when the next screen loads we print it should show the desired

157
00:09:12,520 --> 00:09:13,980
league that we selected.

158
00:09:14,020 --> 00:09:16,110
Hypothetically if we wrote the code correctly

159
00:09:24,310 --> 00:09:24,660
OK.

160
00:09:24,710 --> 00:09:26,790
So get started.

161
00:09:26,840 --> 00:09:28,590
Let's go ahead and select the women's league.

162
00:09:28,880 --> 00:09:32,330
And when I click Next I should see women's printed here on the screen.

163
00:09:33,830 --> 00:09:35,590
And sure enough it was OK.

164
00:09:35,630 --> 00:09:37,340
So are you with me so far.

165
00:09:37,670 --> 00:09:40,720
What happened was we triggered her segue.

166
00:09:40,790 --> 00:09:43,040
So right here.

167
00:09:43,060 --> 00:09:44,340
See this performed segue.

168
00:09:44,380 --> 00:09:46,360
We triggered that from the next button right.

169
00:09:46,420 --> 00:09:48,710
And we said let's load the skill AVC So this is called.

170
00:09:49,000 --> 00:09:52,500
And then the very next thing that's called is prepare for segue.

171
00:09:52,540 --> 00:09:57,790
And this is where we pass data from this view controller into the next using this code here and we set

172
00:09:57,790 --> 00:09:58,750
the data that we need.

173
00:09:59,810 --> 00:10:03,560
And it doesn't have to be in this very well we could call the function that sets it in a variable but

174
00:10:03,560 --> 00:10:10,070
somehow some way we need to set data on the destination view controller then the destination view controller

175
00:10:10,070 --> 00:10:14,560
which was the skill Visi in view to load our data is already ready to go.

176
00:10:14,600 --> 00:10:16,580
And you could print it and use it accordingly.

177
00:10:16,580 --> 00:10:19,620
So we've kind of just built the app right.

178
00:10:19,690 --> 00:10:22,460
What we've done is we built an app that has three screens.

179
00:10:22,460 --> 00:10:24,610
We've learned how to set data from some fields.

180
00:10:24,610 --> 00:10:30,390
We selected a field we stored in a model and then we passed that data model between View Controller

181
00:10:30,410 --> 00:10:34,910
so we can use it and you could continue to do this we could call another performed from Segway and another

182
00:10:34,910 --> 00:10:38,860
prepare and pass it on to the next one and pass it on to the next one and then the next one.

183
00:10:38,870 --> 00:10:42,730
Now if you're thinking that's a lot of work do we really want to pass data from view control of your

184
00:10:42,740 --> 00:10:43,850
controller.

185
00:10:43,850 --> 00:10:48,350
And I would say no later on you're going to learn how to work with some different design patterns like

186
00:10:48,350 --> 00:10:51,780
the singleton pattern where you have a central source that holds all your data.

187
00:10:52,040 --> 00:10:54,130
And that's a lot nicer than passing things through.

188
00:10:54,140 --> 00:10:56,290
But this is a very common practice as well too.

189
00:10:56,360 --> 00:11:02,360
And this is in the Apple documentation we've done everything exactly as Apple teaches in their documentation

190
00:11:02,390 --> 00:11:05,470
and it's very useful So that's it for now.

191
00:11:05,480 --> 00:11:07,130
This is really good stuff.

192
00:11:07,130 --> 00:11:11,960
Core foundational things that you need to know everything that I've taught you on the swish app.

193
00:11:11,960 --> 00:11:16,160
You have to know it now and if you ever want to get a job in industry you have to know everything we've

194
00:11:16,160 --> 00:11:17,440
done in this app by heart.

195
00:11:17,540 --> 00:11:18,130
OK.

196
00:11:18,380 --> 00:11:21,530
There's nothing overly complex but the things that we have learned here you have to know by heart in

197
00:11:21,530 --> 00:11:25,820
order to get a job if you know if you don't know these things that are in this lesson you're not ready

198
00:11:25,820 --> 00:11:30,270
for a job yet so you're going to need to master these very basic principles because this is what you're

199
00:11:30,270 --> 00:11:32,660
going to doing most of the time in your app development.

200
00:11:32,660 --> 00:11:34,010
So that's it for now.

201
00:11:34,010 --> 00:11:36,520
Marc price at Dev slopes dot com.

202
00:11:36,560 --> 00:11:37,880
Movin on and forward.

