1
00:00:00,506 --> 00:00:06,156
[ Silence ]

2
00:00:06,656 --> 00:00:07,636
>> Welcome back.

3
00:00:08,386 --> 00:00:10,956
First thing first, we
have a new team member,

4
00:00:10,956 --> 00:00:12,246
Chris Gerber [assumed spelling].

5
00:00:12,246 --> 00:00:14,516
So, some of you probably
already heard from him

6
00:00:14,516 --> 00:00:21,646
as he being your point
person, but he's here to help.

7
00:00:21,646 --> 00:00:25,096
Second, don't forget that your
project is due next Wednesday.

8
00:00:25,366 --> 00:00:30,676
So we are diving into
iOS/Objective-C today.

9
00:00:31,056 --> 00:00:34,266
But you still need to keep up
with your project that still has

10
00:00:34,266 --> 00:00:35,756
to do with the JavaScript
and HTML.

11
00:00:36,446 --> 00:00:40,036
OK. So, first we're going
to recommend some books.

12
00:00:40,646 --> 00:00:42,516
David always recommends
this one.

13
00:00:42,516 --> 00:00:45,816
You're going to want to
make sure that your book is

14
00:00:45,816 --> 00:00:48,106
up to date with all
the new iOS versions.

15
00:00:48,376 --> 00:00:52,686
So iOS 7, it was a very
recent development.

16
00:00:52,686 --> 00:00:55,066
We are only going to be
be dealing with iOS 6,

17
00:00:55,066 --> 00:00:56,336
plus I think iOS 7 isn't

18
00:00:56,336 --> 00:00:59,836
like officially available
for couple of months.

19
00:00:59,836 --> 00:01:02,746
So, iOS 6 is what you're
going to want to be using.

20
00:01:03,056 --> 00:01:05,436
When you start going
back versions of iOS,

21
00:01:05,726 --> 00:01:07,596
things start changing
pretty significantly.

22
00:01:07,596 --> 00:01:10,736
You don't want to go
back to like iOS 4 before

23
00:01:10,736 --> 00:01:12,406
because things are going to
be substantially different.

24
00:01:12,876 --> 00:01:14,776
So, look for iOS 6 things.

25
00:01:15,196 --> 00:01:17,856
Another book that
I've never looked at

26
00:01:17,856 --> 00:01:22,516
but is supposedly also
pretty good is this one.

27
00:01:22,516 --> 00:01:25,336
And I added this
slide just before

28
00:01:25,336 --> 00:01:27,876
because this is one
the actually use just

29
00:01:27,876 --> 00:01:29,426
to catch myself up
with everything.

30
00:01:30,526 --> 00:01:34,916
It's a pre-preview of a book and
I found it to be pretty good.

31
00:01:35,206 --> 00:01:38,626
I feel obligated to say like
he does at the top of the page

32
00:01:38,626 --> 00:01:40,106
like if you find it
useful you might want

33
00:01:40,106 --> 00:01:41,876
to consider actually
buying the book.

34
00:01:42,506 --> 00:01:46,746
But that pretty much
everything is in here.

35
00:01:46,896 --> 00:01:49,556
OK. So before we get into
Objective-C, you first need

36
00:01:49,556 --> 00:01:53,116
to touch on C. So, it's
important to remember

37
00:01:53,116 --> 00:01:57,156
that Objective-C is a proper
super set of regular C.

38
00:01:57,446 --> 00:01:59,966
So like any Objective-C
program that you write,

39
00:01:59,966 --> 00:02:03,496
you could write valid
C all throughout the

40
00:02:03,496 --> 00:02:04,336
Objective-C program.

41
00:02:04,416 --> 00:02:05,696
We're not literally everywhere.

42
00:02:06,206 --> 00:02:08,196
But everything you know

43
00:02:08,196 --> 00:02:10,696
from C is still applicable
in Objective-C.

44
00:02:10,836 --> 00:02:15,436
So, first, show of hands,
who know C or any C?

45
00:02:15,436 --> 00:02:19,186
All right, that's better
than I was anticipating.

46
00:02:19,596 --> 00:02:23,256
And another show of hands,
who has any experience

47
00:02:23,256 --> 00:02:25,296
with any object-oriented
language?

48
00:02:25,796 --> 00:02:27,456
All right.

49
00:02:28,606 --> 00:02:32,816
Cool. So, we'll do a
really quick intro to C just

50
00:02:32,816 --> 00:02:34,096
to refresh you on everything.

51
00:02:34,096 --> 00:02:38,006
You'll probably recognize that
a lot of it is pretty similar

52
00:02:38,006 --> 00:02:40,846
to JavaScript, so probably
still pretty fresh.

53
00:02:42,086 --> 00:02:45,086
So, your very standard
"Hello World" program.

54
00:02:45,766 --> 00:02:49,036
What does hash includes,
standard IO.H2.

55
00:02:49,036 --> 00:02:50,396
What is standard IO.H?

56
00:02:51,331 --> 00:02:53,331
[ Pause ]

57
00:02:53,646 --> 00:02:55,246
I Think I might have
heard someone say.

58
00:02:55,246 --> 00:02:56,706
But it's a header file.

59
00:02:56,896 --> 00:02:59,446
It literally just
contains like--

60
00:03:00,106 --> 00:03:04,466
we'll see like the
declarations of the functions.

61
00:03:04,776 --> 00:03:06,426
So we'll see more
examples of those later.

62
00:03:06,426 --> 00:03:09,746
Those are still going to be
pretty important in Objective-C.œ

63
00:03:10,336 --> 00:03:12,746
Here we see, we are
declaring a main function.

64
00:03:12,746 --> 00:03:14,566
So just like in C we
declare a main function,

65
00:03:14,566 --> 00:03:17,116
in Objective-C you are also
going to have a name function.

66
00:03:17,856 --> 00:03:23,526
And what are these guys, int
argc and con char star argv?

67
00:03:23,526 --> 00:03:26,546
What are those?

68
00:03:26,766 --> 00:03:27,666
Command line arguments.

69
00:03:27,986 --> 00:03:31,426
So int argc is how many
arguments were passed

70
00:03:31,426 --> 00:03:34,656
at the command line and
con char star argv are--

71
00:03:34,656 --> 00:03:36,436
what those arguments were.

72
00:03:37,176 --> 00:03:39,656
And then we just
printf, will be--

73
00:03:39,656 --> 00:03:41,016
we'll see that once
we got to Objective-C.

74
00:03:41,016 --> 00:03:43,076
We're not going to be
really using printf anymore,

75
00:03:43,536 --> 00:03:45,606
but pretty standard program.

76
00:03:46,966 --> 00:03:49,696
So statements-- that
is a statement.

77
00:03:50,876 --> 00:03:54,316
Variables, you just declare
them int n or say int n equal 0.

78
00:03:54,316 --> 00:03:57,256
That's going to be the same
when we get to Objective-C.

79
00:03:57,576 --> 00:04:00,486
We have all of our regular
primitive date types, char,

80
00:04:00,486 --> 00:04:05,356
double float, int long, and then
the modifiers like unsigned int

81
00:04:05,356 --> 00:04:08,756
and unsigned double, and printf.

82
00:04:09,326 --> 00:04:14,336
So, if we-- let's go into--

83
00:04:15,576 --> 00:04:17,836
We'll have a whole bunch
of example programs up.

84
00:04:18,896 --> 00:04:21,876
And we have a whole bunch
in C and Objective-C.

85
00:04:21,876 --> 00:04:24,866
We're not going to go through
all of the C ones because a lot

86
00:04:24,866 --> 00:04:26,046
of them will just be tedious.

87
00:04:26,386 --> 00:04:28,986
But if you feel you need a
refresher, you can just go back

88
00:04:28,986 --> 00:04:29,846
and look at all these.

89
00:04:30,426 --> 00:04:34,956
So, we'll look first
C one, hello C.

90
00:04:35,516 --> 00:04:38,956
[ Pause ]

91
00:04:39,456 --> 00:04:43,676
OK. So, this is what we
just saw before up there.

92
00:04:44,206 --> 00:04:45,566
And printf, remember,

93
00:04:45,676 --> 00:04:48,806
takes these modifiers
such as percent S.

94
00:04:49,516 --> 00:04:52,416
[ Pause ]

95
00:04:52,916 --> 00:04:57,886
That are going to then be a
comma separated list of things

96
00:04:57,886 --> 00:05:00,206
at the end of the printf call
that you're going to fill in.

97
00:05:00,506 --> 00:05:02,986
So like percent S, it's
expecting a string to fill

98
00:05:02,986 --> 00:05:06,696
in that percent S. And if
instead we did int n equals 50,

99
00:05:07,366 --> 00:05:11,886
then we could do percent D
and pass in the variable N.

100
00:05:12,316 --> 00:05:15,056
So that's how we're going
to use printf with--

101
00:05:15,056 --> 00:05:18,296
that's how we print variables.

102
00:05:18,666 --> 00:05:22,626
And you'll see that these
format specifiers just percent S

103
00:05:22,626 --> 00:05:24,326
and S percent D for integer.

104
00:05:25,216 --> 00:05:28,706
Those are going to carry over
into some Objective-C areas.

105
00:05:29,516 --> 00:05:34,576
[ Pause ]

106
00:05:35,076 --> 00:05:37,336
Plus, all of these primitive
data types, remember,

107
00:05:37,336 --> 00:05:41,566
that Objective-C is a super
set of C, these still exist.

108
00:05:42,016 --> 00:05:43,106
[ Inaudible Remark ]

109
00:05:43,106 --> 00:05:43,236
Yeah?

110
00:05:43,236 --> 00:05:44,706
>> Do we still we have link to
the book, the increment book?

111
00:05:47,016 --> 00:05:48,266
[ Inaudible Remark ]

112
00:05:48,266 --> 00:05:49,856
>> The slide will
be online later.

113
00:05:49,856 --> 00:05:51,086
But if you want to
look it up now--

114
00:05:51,836 --> 00:05:52,976
>> Thank you.

115
00:05:53,206 --> 00:05:56,426
>> Yeah. And feel free to stop
me at any point with questions.

116
00:05:57,516 --> 00:06:14,156
[ Pause ]

117
00:06:14,656 --> 00:06:15,196
It's good.

118
00:06:15,766 --> 00:06:17,096
Oh, yeah. OK.

119
00:06:19,096 --> 00:06:24,636
So the Boolean expressions,
the very simple ones

120
00:06:24,636 --> 00:06:26,536
that you should-- all be
used to at this point.

121
00:06:27,356 --> 00:06:29,786
Conditions, we have
a regular if-elses.

122
00:06:29,786 --> 00:06:31,836
Loops, we have for loops.

123
00:06:31,836 --> 00:06:36,116
So in JavaScript, you also
have the, like, for in.

124
00:06:36,286 --> 00:06:38,176
You'll also see that
in Objective-C.

125
00:06:38,206 --> 00:06:39,666
Regular C doesn't' have that.

126
00:06:39,826 --> 00:06:41,866
So we have a regular for loop,

127
00:06:41,866 --> 00:06:43,836
regular while loop,
regular do-while.

128
00:06:44,266 --> 00:06:47,436
And all of those will still
be usable in Objective-C.

129
00:06:47,706 --> 00:06:52,216
So, casting is an interesting
case where it's exactly the same

130
00:06:52,216 --> 00:06:53,656
between C and Objective-C.

131
00:06:53,886 --> 00:06:55,976
You're going to see, you might
be needing it a lot more once

132
00:06:55,976 --> 00:06:57,036
you get to Objective-C.

133
00:06:57,366 --> 00:07:02,506
So, in C, there are only so
many cases I can possibly think

134
00:07:02,506 --> 00:07:03,646
of where it wants a cast.

135
00:07:04,026 --> 00:07:07,576
Like one example is if I want
to do like 3 divided by 5.

136
00:07:07,906 --> 00:07:10,346
You know, how like if we just
do the integers then 3 divided

137
00:07:10,346 --> 00:07:11,786
by 5 is going to be 0.

138
00:07:12,086 --> 00:07:14,976
So I might want to cast 3 to a
float so that it actually does

139
00:07:14,976 --> 00:07:16,636
like floating point division.

140
00:07:16,956 --> 00:07:19,896
So that's one scenario I can
think of where I want to cast.

141
00:07:19,936 --> 00:07:22,576
Once we get to Objective-C and
we start dealing with classes

142
00:07:22,576 --> 00:07:26,646
and things, then we're going to
want to cast like we'll see more

143
00:07:26,646 --> 00:07:28,396
of this but like
supertypes to subtypes

144
00:07:28,396 --> 00:07:29,856
and subtypes to supertypes.

145
00:07:30,196 --> 00:07:34,006
So casting, you'll want
to be comfortable with it.

146
00:07:34,476 --> 00:07:42,236
And then Pointers, they won't
be as important in Objective-C

147
00:07:42,236 --> 00:07:44,616
but you're still going to
have to deal with them.

148
00:07:44,616 --> 00:07:49,706
So, we have just the
type of a pointer--

149
00:07:49,836 --> 00:07:50,976
let's actually look
just an example.

150
00:07:51,516 --> 00:07:56,386
[ Pause ]

151
00:07:56,886 --> 00:07:57,236
All right.

152
00:07:57,236 --> 00:08:01,666
We'll go over the typical
example of a swap function.

153
00:08:02,446 --> 00:08:04,446
[ Pause ]

154
00:08:04,876 --> 00:08:06,976
That's here.

155
00:08:07,046 --> 00:08:10,226
Oh. So this line, like, hint

156
00:08:10,226 --> 00:08:14,896
to that before is swap line
is the function's prototype.

157
00:08:15,316 --> 00:08:18,006
So those are the sort of things
that we'll see in header files

158
00:08:18,006 --> 00:08:23,016
like standard IO.H. We'll also
see like struct definitions.

159
00:08:23,016 --> 00:08:23,926
We'll get to structs.

160
00:08:24,316 --> 00:08:26,226
We'll see hash define things.

161
00:08:28,336 --> 00:08:33,416
So, here in main, we declare
variable X and variable Y,

162
00:08:33,416 --> 00:08:37,746
and then we print X, print Y.
And now we want to swap X and Y.

163
00:08:38,126 --> 00:08:42,166
So ideally, after this swap
line, X will have the value 1

164
00:08:42,166 --> 00:08:43,496
and Y will have the value 0.

165
00:08:43,626 --> 00:08:44,816
They will have swap values.

166
00:08:45,746 --> 00:08:48,016
And I won't actually run it.

167
00:08:48,306 --> 00:08:51,596
But trust me when I say
that this will not succeed.

168
00:08:51,986 --> 00:08:55,756
Looking at the actual swap
function, we see it takes A

169
00:08:55,786 --> 00:09:01,506
and B, so we're passing X for
A and Y for B. We then store A

170
00:09:01,506 --> 00:09:05,526
in a temporary variable, put B
into A, and put the original A

171
00:09:05,526 --> 00:09:10,366
into B. So, why this doesn't
work is it's important

172
00:09:10,366 --> 00:09:12,596
to remember that everything in C

173
00:09:12,936 --> 00:09:15,466
when you pass its way
function it's always a copy

174
00:09:15,466 --> 00:09:16,566
of the thing being passed.

175
00:09:17,136 --> 00:09:20,646
And so here, what we're actually
passing to a swap is a copy

176
00:09:20,646 --> 00:09:23,056
of the value 0 and the
copy of the value 1.

177
00:09:23,626 --> 00:09:27,476
And so down in the swap
function when we say A equals B,

178
00:09:27,606 --> 00:09:30,946
we're just modifying the
copy of those values.

179
00:09:31,016 --> 00:09:35,176
We're not modifying the
original X and Y. So here's

180
00:09:35,176 --> 00:09:36,746
where pointers become important.

181
00:09:37,516 --> 00:09:50,756
[ Pause ]

182
00:09:51,256 --> 00:09:55,336
So, here, notice the
function prototype is changed.

183
00:09:55,946 --> 00:10:00,146
We now are passing int star A.
So as soon as you see int star

184
00:10:00,146 --> 00:10:01,756
or char star, anything star,

185
00:10:01,756 --> 00:10:03,086
you know you're dealing
with a pointer.

186
00:10:04,356 --> 00:10:05,976
And it's coming down here.

187
00:10:05,976 --> 00:10:09,606
We're no longer passing X and
Y. We're passing ampersand X

188
00:10:09,606 --> 00:10:12,476
and ampersand Y. So ampersand,

189
00:10:12,476 --> 00:10:16,256
I usually read literarily
as address of.

190
00:10:16,456 --> 00:10:19,706
So we're passing the address of
X and we're passing the address

191
00:10:19,706 --> 00:10:26,526
of Y. And so-- Oh, and
it's-- since X is an int,

192
00:10:27,056 --> 00:10:29,156
then when you get the
address of an int,

193
00:10:29,636 --> 00:10:32,356
the type of that
address is an int star.

194
00:10:32,766 --> 00:10:34,806
We had a char and-- with
an ampersand before it,

195
00:10:34,806 --> 00:10:36,006
then we would have a char star.

196
00:10:36,556 --> 00:10:40,736
So coming down into the swap
function, again, this is going

197
00:10:40,736 --> 00:10:42,856
to take an int star
and an int star.

198
00:10:43,276 --> 00:10:47,566
And here, it's unfortunately
somewhat confusing

199
00:10:47,566 --> 00:10:50,276
that there are two
uses for the star.

200
00:10:50,896 --> 00:10:52,546
The first use for the star is

201
00:10:52,546 --> 00:10:54,146
to declare something
as a pointer type.

202
00:10:54,566 --> 00:10:55,966
So when you see int star B,

203
00:10:55,966 --> 00:10:59,096
you know that B is a
pointer to an integer.

204
00:10:59,646 --> 00:11:02,556
When you do not see a type, like
right here, when you say star A,

205
00:11:02,726 --> 00:11:05,086
we are de-referencing
that pointer.

206
00:11:05,606 --> 00:11:12,176
So if B points to Y
and A points to X,

207
00:11:12,556 --> 00:11:18,416
then star A is referencing the
original X. And so temp is going

208
00:11:18,416 --> 00:11:22,696
to store the original value
of X. Star A equals star B,

209
00:11:22,956 --> 00:11:25,516
we're going to go to
the original value of X,

210
00:11:25,516 --> 00:11:27,586
like the original
X and we're going

211
00:11:27,586 --> 00:11:30,566
to store there the
original Y. And so,

212
00:11:30,566 --> 00:11:32,516
X is actually been changed now.

213
00:11:32,886 --> 00:11:35,316
And here, we're doing
the same thing.

214
00:11:35,316 --> 00:11:40,686
We're going to Y and storing the
original value of X. Questions?

215
00:11:41,451 --> 00:11:43,451
[ Pause ]

216
00:11:43,886 --> 00:11:48,126
OK. So, in Objective-C, a
lot of this is going to be--

217
00:11:48,816 --> 00:11:52,466
you won't be needing to do as
many explicit de-references.

218
00:11:52,826 --> 00:11:53,796
But it's important to remember

219
00:11:53,796 --> 00:11:55,476
that when you're
passing these things

220
00:11:55,716 --> 00:11:58,706
that object themselves are
pretty much like pointers.

221
00:11:59,136 --> 00:12:03,186
And so if like you're passing
object to a function and in

222
00:12:03,186 --> 00:12:04,836
that function you
modify the object,

223
00:12:05,116 --> 00:12:06,866
the original object
will had been modified

224
00:12:07,196 --> 00:12:07,956
and we're going to see that.

225
00:12:08,516 --> 00:12:15,736
[ Pause ]

226
00:12:16,236 --> 00:12:17,736
And just for fun,
I threw in the end

227
00:12:17,736 --> 00:12:20,876
that char star star is also
an example of a pointer.

228
00:12:21,246 --> 00:12:23,676
It just happens to be
a pointer that points

229
00:12:23,676 --> 00:12:26,186
to a pointer that points a char.

230
00:12:27,336 --> 00:12:34,276
So a good example of that is
just our command line arguments,

231
00:12:34,626 --> 00:12:35,316
argv.

232
00:12:35,716 --> 00:12:40,746
So, ignoring the fact that it's
bracket syntax, there are some--

233
00:12:40,966 --> 00:12:42,356
this isn't entirely true.

234
00:12:42,606 --> 00:12:43,926
But in a lot of scenarios,

235
00:12:43,926 --> 00:12:46,306
arrays and pointers are
pretty much equivalent.

236
00:12:46,606 --> 00:12:49,876
So con char star argv bracket,
we could have also said

237
00:12:49,876 --> 00:12:53,466
as con char star star
argv, which makes sense

238
00:12:53,466 --> 00:12:57,396
since argv is an
array of strings

239
00:12:57,826 --> 00:13:01,576
and we represent a string as a
pointer to a single character.

240
00:13:03,156 --> 00:13:03,966
Questions?

241
00:13:04,516 --> 00:13:13,506
[ Pause ]

242
00:13:14,006 --> 00:13:19,206
OK. So, structs, so
it'll be important

243
00:13:19,206 --> 00:13:20,316
to actually remember structs

244
00:13:20,866 --> 00:13:23,446
because you will still be
dealing with them in Objective-C

245
00:13:23,446 --> 00:13:28,156
and a lot of the iOS code will
be dealing with some structs.

246
00:13:28,606 --> 00:13:30,836
Unfortunately, it can be
somewhat confusing 'cause you

247
00:13:30,836 --> 00:13:33,626
might forget when you're dealing
with a struct versus an object

248
00:13:33,626 --> 00:13:34,696
since they are different.

249
00:13:35,096 --> 00:13:38,536
But let's look an
example of using a struct.

250
00:13:38,676 --> 00:13:42,926
Remember, struct is pure C
and objects are Objective-C.

251
00:13:43,516 --> 00:13:45,516
[ Pause ]

252
00:13:46,016 --> 00:13:47,000
[ Inaudible Remark ]

253
00:13:47,836 --> 00:13:47,976
Yes.

254
00:13:48,516 --> 00:13:50,586
[ Inaudible Remark ]

255
00:13:51,086 --> 00:13:52,466
So you can.

256
00:13:52,986 --> 00:13:57,706
Once we get to Objective-C,
you can, yes.

257
00:13:58,316 --> 00:14:02,426
It's-- A struct is literarily
just a wrapper for a--

258
00:14:02,996 --> 00:14:05,496
like a group of variables.

259
00:14:05,556 --> 00:14:07,946
So you're going to
have a variable point

260
00:14:07,946 --> 00:14:10,116
to an object then you
can store that variable

261
00:14:10,116 --> 00:14:13,686
in a struct just as well.

262
00:14:13,866 --> 00:14:16,506
Looking at a struct example
that doesn't use objects.

263
00:14:17,516 --> 00:14:21,736
[ Pause ]

264
00:14:22,236 --> 00:14:26,626
We see here this
typedef struct student.

265
00:14:27,516 --> 00:14:33,976
So the only reason for
the typedef is so that--

266
00:14:34,456 --> 00:14:35,806
[inaudible] technically
it's a student here.

267
00:14:35,806 --> 00:14:37,376
But the only reason
for the typedef is

268
00:14:37,376 --> 00:14:39,386
so that whenever we want
to declare a variable

269
00:14:39,386 --> 00:14:43,376
of this struct type, if we just
do this, now we have to say,

270
00:14:43,376 --> 00:14:45,656
struct student X and
struct student Y.

271
00:14:46,076 --> 00:14:48,416
So the typedef just
saves us that.

272
00:14:48,776 --> 00:14:52,256
And now I can just
say struct student--

273
00:14:52,256 --> 00:14:56,626
or now we can just say
student X and student Y.

274
00:14:57,016 --> 00:14:58,556
So what is inside
of this struct?

275
00:14:58,886 --> 00:15:01,086
We have an age and
we have a name.

276
00:15:01,616 --> 00:15:04,586
And so, these are exactly
what structs will look

277
00:15:04,586 --> 00:15:06,226
like in Objective-C.

278
00:15:06,706 --> 00:15:11,156
And coming down here, we
see we declare a variable

279
00:15:11,156 --> 00:15:16,856
of this struct type, Alice, and
we're going to use .notation

280
00:15:16,856 --> 00:15:18,846
to access the fields
in that struck.

281
00:15:19,186 --> 00:15:23,396
So, Alice.age equals 20 and
Alice.name equals Alice.

282
00:15:23,446 --> 00:15:25,346
And we could do the same
thing, we just declare Bob

283
00:15:25,756 --> 00:15:30,526
and that's pretty
much like everything

284
00:15:30,526 --> 00:15:32,136
about Structs that
you will need.

285
00:15:32,646 --> 00:15:34,506
So, questions?

286
00:15:34,506 --> 00:15:39,046
Oh, and then if you want, look
at degree [phonetic] function.

287
00:15:39,596 --> 00:15:43,886
Up here we are storing
inside of Bob at age 21

288
00:15:44,186 --> 00:15:47,446
and down here we are
accessing Bob.age.

289
00:15:50,886 --> 00:15:56,116
OK. Next, we will
also have enums

290
00:15:56,586 --> 00:15:59,556
which are somewhat more
important in Objective-C

291
00:15:59,556 --> 00:16:03,416
than they were in-- perhaps
you've used in regular C

292
00:16:03,666 --> 00:16:08,996
since there's a lot of the-- a
lot of code you'll be working

293
00:16:08,996 --> 00:16:10,486
with uses a lot of enums.

294
00:16:11,226 --> 00:16:12,926
So, what does an enum look like?

295
00:16:12,926 --> 00:16:16,596
So, it's like a struct and again
we're just using typedef so that

296
00:16:16,596 --> 00:16:18,186
when we want to declare
a variable

297
00:16:18,186 --> 00:16:22,046
of this enum type we don't
have to say enum genders X

298
00:16:22,046 --> 00:16:24,736
and enum genders Y. Thanks
to the typedef you're going

299
00:16:24,736 --> 00:16:28,226
to say genders X
and genders Y. So,

300
00:16:29,356 --> 00:16:31,906
enums aren't strictly necessary.

301
00:16:32,226 --> 00:16:36,526
We could instead do
hash define female as 0

302
00:16:36,846 --> 00:16:39,026
and hash define male as 1.

303
00:16:39,356 --> 00:16:42,586
And hash define just declares
this to be a constant.

304
00:16:42,966 --> 00:16:46,736
So right now, if
I got rid of this,

305
00:16:46,876 --> 00:16:50,586
the two programs will be almost
equivalent except for the fact

306
00:16:50,586 --> 00:16:54,576
that I just got rid
of the genders type.

307
00:16:54,676 --> 00:16:57,766
So, why do we prefer enums
over these hash defines?

308
00:16:58,106 --> 00:17:01,886
Because if I add a whole bunch
of hash defines something two,

309
00:17:01,886 --> 00:17:04,106
something three, something
four and then I decided

310
00:17:04,106 --> 00:17:05,886
to like, reorder them.

311
00:17:05,886 --> 00:17:09,536
Then I would have to like change
the numbers on all the rest

312
00:17:09,536 --> 00:17:14,416
of them and also it's just a
convenient that I don't have

313
00:17:14,416 --> 00:17:16,436
to explicitly list the
numbers on all these things.

314
00:17:17,636 --> 00:17:21,706
Once we get to Objective-C and
I think more specifically Xcode,

315
00:17:21,886 --> 00:17:26,726
there's also some-- well,
it's coming down here.

316
00:17:27,836 --> 00:17:30,106
We see that we have changed
this struct to now not--

317
00:17:30,646 --> 00:17:32,316
or now it takes a
name and a gender.

318
00:17:32,876 --> 00:17:36,096
So, Alice is going to be-- is
going to be given the name Alice

319
00:17:36,096 --> 00:17:37,806
and it's going to be
given the gender female.

320
00:17:38,296 --> 00:17:41,926
So, it's important to remember
in C and also Objective-C

321
00:17:42,176 --> 00:17:45,046
that there is no enforcement
of the gender's type.

322
00:17:45,646 --> 00:17:48,056
So, I am just as easily able

323
00:17:48,056 --> 00:17:50,486
to put a hundred
underneath the hood

324
00:17:50,486 --> 00:17:52,516
and enum is literally
just an integer.

325
00:17:52,706 --> 00:17:57,446
So, even though a hundred
is not a valid genders,

326
00:17:58,266 --> 00:18:00,006
it will let me do this.

327
00:18:01,006 --> 00:18:04,596
But in Xcode, generally
it will--

328
00:18:05,036 --> 00:18:08,696
it will yell at you like
visually if you're using things

329
00:18:08,696 --> 00:18:10,146
that aren't correct enum types.

330
00:18:10,816 --> 00:18:13,656
So, you'll wan to use
these enums correctly.

331
00:18:14,286 --> 00:18:19,196
And that's pretty
much it for enums.

332
00:18:20,016 --> 00:18:20,476
Questions?

333
00:18:21,516 --> 00:18:24,276
[ Pause ]

334
00:18:24,776 --> 00:18:37,366
OK. So then, arrays, regular
C arrays are still going

335
00:18:37,366 --> 00:18:39,356
to work in Objective-C.

336
00:18:39,356 --> 00:18:40,626
You're also going to see

337
00:18:40,626 --> 00:18:42,596
that there are these
new array classes

338
00:18:42,596 --> 00:18:44,496
that you're probably going
to want to get used to.

339
00:18:44,496 --> 00:18:46,816
So, you might not be
using them that much.

340
00:18:47,606 --> 00:18:49,956
Just to look at an example
of using the arrays.

341
00:18:50,516 --> 00:18:55,576
[ Pause ]

342
00:18:56,076 --> 00:19:00,866
So, here we declare
the variable int 10.

343
00:19:01,066 --> 00:19:05,056
We print enter number of exams
and then scanf is just a way

344
00:19:05,056 --> 00:19:07,376
of grabbing input
from the keyboard.

345
00:19:07,906 --> 00:19:11,836
So here we are scanf'ing
for some integer,

346
00:19:11,986 --> 00:19:14,366
this percent D carries
over from printdef.

347
00:19:14,366 --> 00:19:15,656
It's looking for an integer.

348
00:19:16,166 --> 00:19:17,666
And why do we need to--

349
00:19:17,666 --> 00:19:21,896
why do we need to pass
ampersand N and not just N?

350
00:19:22,326 --> 00:19:23,876
Why would this never work?

351
00:19:24,516 --> 00:19:28,516
[ Pause ]

352
00:19:29,016 --> 00:19:30,000
[ Inaudible Remark ]

353
00:19:31,296 --> 00:19:34,646
Yeah. So inside of
this scanf function,

354
00:19:34,646 --> 00:19:36,816
just like we saw before
the swap function,

355
00:19:37,076 --> 00:19:42,006
ideally after this point in time
N equals whatever number I type

356
00:19:42,006 --> 00:19:42,976
at the keyboard.

357
00:19:43,666 --> 00:19:50,916
So, scanf needs to actually
change this N. Scanf can't

358
00:19:51,006 --> 00:19:54,096
change that without
a pointer to that N,

359
00:19:54,096 --> 00:19:56,616
without having the address
of that N. Otherwise,

360
00:19:56,616 --> 00:19:59,976
if I just did this and
we'll first do in compile.

361
00:20:00,056 --> 00:20:03,366
But assuming it did accept
that, it would just try

362
00:20:03,366 --> 00:20:09,066
to set the local variable N
to the keyboard entered value

363
00:20:09,296 --> 00:20:11,866
and this N would
remain unchanged.

364
00:20:12,306 --> 00:20:16,946
So, we need to pass the address.

365
00:20:17,086 --> 00:20:22,106
Here we are declaring an array
and that array is going to be

366
00:20:22,106 --> 00:20:24,566
of size N. So we're
going to have N integers.

367
00:20:25,026 --> 00:20:27,276
And this is all going
on the stack so far.

368
00:20:28,456 --> 00:20:34,486
And then we're iterating over
the N values entering a grade

369
00:20:34,486 --> 00:20:36,986
into each individual
spot in the array.

370
00:20:37,456 --> 00:20:39,026
So really, this program
is going to behave

371
00:20:39,026 --> 00:20:40,866
like how many grades
you want to enter?

372
00:20:41,366 --> 00:20:43,646
Three. What is the first grade?

373
00:20:43,876 --> 00:20:44,806
What is the second grade?

374
00:20:44,936 --> 00:20:45,756
What is the third grade?

375
00:20:46,226 --> 00:20:47,496
So, you enter each grade.

376
00:20:48,456 --> 00:20:54,666
And also we have to-- So
grades I is the Ith position

377
00:20:54,666 --> 00:20:58,566
in the array and then we also
need to pass that by reference.

378
00:20:58,616 --> 00:21:01,936
We need to pass the pointer to
grades I for the same reason

379
00:21:01,936 --> 00:21:03,476
that we needed to
pass the pointer to N.

380
00:21:04,516 --> 00:21:06,936
[ Pause ]

381
00:21:07,436 --> 00:21:07,976
Questions?

382
00:21:08,516 --> 00:21:21,156
[ Pause ]

383
00:21:21,656 --> 00:21:23,896
OK. So, memory management.

384
00:21:24,606 --> 00:21:30,516
It's one of the bigger problems
in C and for the most part,

385
00:21:30,516 --> 00:21:32,676
you are going to have to
deal with it in Objective-C.

386
00:21:32,916 --> 00:21:35,416
There are some small things
you'll have to think about

387
00:21:35,476 --> 00:21:37,006
and we'll get to that next week.

388
00:21:37,726 --> 00:21:42,626
But the malloc and free
stuff that you're used to,

389
00:21:43,196 --> 00:21:44,606
you don't really
have to deal with.

390
00:21:44,806 --> 00:21:50,206
So, remember that we have
our stack and our heap.

391
00:21:50,726 --> 00:21:53,206
And so all of our local
variables are always stored

392
00:21:53,206 --> 00:21:53,646
on the stack.

393
00:21:54,576 --> 00:21:59,176
And this means, let's say I
call some function and it needs

394
00:21:59,176 --> 00:22:05,426
to like return some array to
the function that called it.

395
00:22:05,926 --> 00:22:08,536
It can't just return-- it
can't just return an array

396
00:22:08,536 --> 00:22:09,766
on the stack and return it,

397
00:22:09,846 --> 00:22:11,936
because once the function
returns that array is gone.

398
00:22:12,106 --> 00:22:13,886
That's the point of it
being a local variable.

399
00:22:13,886 --> 00:22:14,566
The array is gone.

400
00:22:15,076 --> 00:22:19,046
So, malloc, what that does,
is allocate space on the heap

401
00:22:19,636 --> 00:22:21,506
and the stuff that out--

402
00:22:21,506 --> 00:22:24,646
that's allocated on the heap
survives past whenever the

403
00:22:24,646 --> 00:22:25,916
function happens to end,

404
00:22:26,176 --> 00:22:28,156
whenever that variable
happens to go out of scope.

405
00:22:28,656 --> 00:22:33,716
And so what malloc and free are
like manual memory management

406
00:22:33,716 --> 00:22:36,576
versus the automatic memory
management of local variables.

407
00:22:37,276 --> 00:22:39,476
And something to keep in mind is

408
00:22:39,476 --> 00:22:42,516
that pretty much all objects
are going to be like this.

409
00:22:42,616 --> 00:22:45,226
All objects are going to
pretty much be malloc.

410
00:22:45,226 --> 00:22:46,276
They're going to go on the heap.

411
00:22:46,706 --> 00:22:51,286
And so they are going
to continue existing

412
00:22:51,336 --> 00:22:54,476
after the scope of
that variable ends.

413
00:22:55,496 --> 00:23:00,166
But thanks to the new
iOS Objective-C features,

414
00:23:00,166 --> 00:23:03,076
you don't really have to think
about freeing that memory.

415
00:23:03,366 --> 00:23:05,476
So we'll see why
that's really useful.

416
00:23:06,516 --> 00:23:09,146
[ Inaudible Remark ]

417
00:23:09,646 --> 00:23:12,486
Yes. So, the new-- it's ARC,

418
00:23:12,486 --> 00:23:14,546
A-R-C is Automatic
Reference Counting

419
00:23:14,926 --> 00:23:17,896
and it will automatically
collect these objects for you.

420
00:23:18,306 --> 00:23:20,856
That was not the case
two or three years ago.

421
00:23:20,856 --> 00:23:20,966
Yeah?

422
00:23:21,516 --> 00:23:25,136
[ Inaudible Remark ]

423
00:23:25,636 --> 00:23:28,056
When you don't want to use ARC?

424
00:23:28,056 --> 00:23:30,906
So, the only thing I
could really think of is

425
00:23:30,906 --> 00:23:36,936
like if you wanted to be
really explicit about the--

426
00:23:37,076 --> 00:23:40,756
like you want to fine-tune your
program, overly optimize it

427
00:23:40,756 --> 00:23:43,176
to make it like run better.

428
00:23:43,416 --> 00:23:49,266
But that's probably not
the beast use of your time.

429
00:23:50,176 --> 00:23:54,066
It's-- You are already
using Objective-C

430
00:23:54,066 --> 00:23:57,206
which there are a lot of
things that that's not doing

431
00:23:57,206 --> 00:23:59,536
in the most optimized
way possible.

432
00:23:59,876 --> 00:24:04,496
So, pretty much any app
nowadays is going to use ARC.

433
00:24:05,516 --> 00:24:09,876
[ Inaudible Remark ]

434
00:24:10,376 --> 00:24:11,976
Oh, so an example--

435
00:24:12,516 --> 00:24:16,766
[ Inaudible Remark ]

436
00:24:17,266 --> 00:24:18,876
It probably would.

437
00:24:19,676 --> 00:24:22,886
But it's not really
worth even trying

438
00:24:22,886 --> 00:24:24,806
to optimize your app for that.

439
00:24:25,016 --> 00:24:26,146
[ Inaudible Remark ]

440
00:24:26,146 --> 00:24:26,346
Yeah.

441
00:24:27,516 --> 00:24:35,356
[ Inaudible Remark ]

442
00:24:35,856 --> 00:24:36,806
No, it will not.

443
00:24:36,806 --> 00:24:38,576
You pretty much shouldn't
be malloc'ing.

444
00:24:39,146 --> 00:24:43,316
I actually wonder, I'm not
a hundred percent sure,

445
00:24:43,316 --> 00:24:47,786
but I imagine it's already
taking very strict control

446
00:24:47,786 --> 00:24:48,926
of what the heap looks like.

447
00:24:48,926 --> 00:24:50,846
And so, if you start
malloc'ing things on top

448
00:24:50,846 --> 00:24:52,166
of what it's trying
to do with the heap,

449
00:24:52,166 --> 00:24:54,156
it might screw it
up but I'm not sure.

450
00:24:55,376 --> 00:25:00,686
You won't need to malloc.

451
00:25:00,686 --> 00:25:02,316
OK. Objective-C.

452
00:25:02,846 --> 00:25:05,396
So C questions before we
move on to Objective-C stuff.

453
00:25:05,796 --> 00:25:05,916
Yeah.

454
00:25:06,336 --> 00:25:08,336
[ Inaudible Remark ]

455
00:25:08,656 --> 00:25:12,236
Oh. So that's-- Well, in C
you would need to use free.

456
00:25:12,426 --> 00:25:16,356
So anything you malloc, at
some point during the life

457
00:25:16,356 --> 00:25:17,876
of your program, you
should also free.

458
00:25:18,966 --> 00:25:22,336
In Objective-C, that's the
thing so things are going

459
00:25:22,336 --> 00:25:26,266
to be somewhat automatically
allocated for you

460
00:25:26,516 --> 00:25:30,386
and they will be automatically
de-allocated for you as long

461
00:25:30,386 --> 00:25:33,756
as you adhere to some
things we'll see next week.

462
00:25:33,896 --> 00:25:40,816
OK. Any other questions about C
before some Objective-C stuff?

463
00:25:40,976 --> 00:25:46,976
OK. So, from our Hello World
in C to our Hello World

464
00:25:46,976 --> 00:25:51,256
in Objective-C, there
aren't too many changes.

465
00:25:51,256 --> 00:25:54,706
You'll see that it looks a
lot like C in a lot of ways.

466
00:25:55,626 --> 00:25:58,016
Unfortunately, the first
word has literally changed.

467
00:25:58,166 --> 00:26:01,516
So what do you think the
difference is between include--

468
00:26:01,686 --> 00:26:03,956
like we used to say hash
include, now we say hash import.

469
00:26:03,956 --> 00:26:05,536
So what do you think
the difference is there?

470
00:26:06,516 --> 00:26:09,976
[ Pause ]

471
00:26:10,476 --> 00:26:14,916
So hash import is
smarter than hash include

472
00:26:14,916 --> 00:26:17,976
and that it will only import
something exactly once.

473
00:26:19,026 --> 00:26:22,946
And so, this used to be an
issue with hash includes

474
00:26:22,946 --> 00:26:25,616
like if you hash include
a header file twice

475
00:26:25,616 --> 00:26:28,556
and you don't take special
precautions in that header file,

476
00:26:28,906 --> 00:26:30,696
then you might duplicate
some definitions

477
00:26:30,696 --> 00:26:31,976
of things and it won't compile.

478
00:26:32,866 --> 00:26:34,636
Hash import, no matter how--

479
00:26:34,636 --> 00:26:36,576
if I said hash import
foundation dot--

480
00:26:36,576 --> 00:26:38,876
or like if I just copy
and paste this line,

481
00:26:39,106 --> 00:26:40,766
the second line wouldn't
do anything.

482
00:26:41,776 --> 00:26:47,416
So, hash import just going
to be more efficient in that.

483
00:26:47,636 --> 00:26:49,876
And the-- another thing

484
00:26:49,876 --> 00:26:53,996
that looks somewhat different
is this @autoreleasepool.

485
00:26:54,406 --> 00:26:57,946
So that's going to be something
we deal with next week.

486
00:26:57,946 --> 00:27:00,636
That has to do with
automatic memory management.

487
00:27:02,676 --> 00:27:06,676
And the one thing to note and
we'll also see it in front

488
00:27:06,676 --> 00:27:11,466
of the Hello World is @ is
a very Objective-C thing.

489
00:27:11,466 --> 00:27:13,376
So like if you see code
and you see an at symbol,

490
00:27:13,586 --> 00:27:15,976
you might be able to reason

491
00:27:15,976 --> 00:27:17,746
that it is Objective-C
'cause you're going

492
00:27:17,746 --> 00:27:19,286
to see this all over the place.

493
00:27:19,616 --> 00:27:23,696
In case of the Hello World,
so normally we would see just

494
00:27:23,696 --> 00:27:27,226
like string hello world by
putting the at symbol in front

495
00:27:27,226 --> 00:27:30,306
of it, we are making it an
Objective-C type string.

496
00:27:30,716 --> 00:27:33,776
More explicitly we are
making it an NS string.

497
00:27:34,066 --> 00:27:35,866
So we're going to see that
in a couple of other places.

498
00:27:36,196 --> 00:27:41,716
But now this-- this at
hello world is an object

499
00:27:41,846 --> 00:27:43,876
that we are passing to
the NS log function.

500
00:27:44,566 --> 00:27:45,656
Let's actually run that.

501
00:27:46,516 --> 00:27:56,076
[ Pause ]

502
00:27:56,576 --> 00:27:59,196
So what is the ridiculous--

503
00:28:00,076 --> 00:28:04,636
and so you will pretty
much never need

504
00:28:04,636 --> 00:28:06,076
to compile at the command line.

505
00:28:06,076 --> 00:28:10,006
Because once we get
into Xcode, that's going

506
00:28:10,006 --> 00:28:12,356
to pretty much be
everything you need.

507
00:28:13,146 --> 00:28:16,066
But here, we have
compiled that program.

508
00:28:16,066 --> 00:28:19,816
It's exactly the same as the
one that we saw in the slide.

509
00:28:20,566 --> 00:28:25,096
And running it, we see
something that looks like this.

510
00:28:26,806 --> 00:28:32,206
So, we see the date, we
see the time, we see main

511
00:28:32,436 --> 00:28:34,706
and then this happens
to be the process ID

512
00:28:34,706 --> 00:28:36,356
and then something called
the "Mac Port" [phonetic]

513
00:28:36,356 --> 00:28:37,576
that you don't have
to care about at all,

514
00:28:37,906 --> 00:28:39,996
and then the actual
thing that we NSLogged.

515
00:28:40,666 --> 00:28:43,116
So, this is what NSLog does.

516
00:28:43,906 --> 00:28:47,876
You will probably be using
NSLog pretty frequently

517
00:28:47,876 --> 00:28:50,656
in your programs
to debug things.

518
00:28:51,046 --> 00:28:53,896
In actual released apps,
you probably want to get rid

519
00:28:53,896 --> 00:28:56,116
of your NSLogs or have
them wrapped in, like,

520
00:28:56,276 --> 00:28:57,596
debug type statements.

521
00:28:58,286 --> 00:28:59,936
Since-- Once you're
on an iPhone,

522
00:28:59,936 --> 00:29:01,416
you pretty much don't
have access

523
00:29:01,416 --> 00:29:02,686
to anything that is NSLogged.

524
00:29:03,416 --> 00:29:05,636
I think theoretically, you
can like plug in your iPhones

525
00:29:05,636 --> 00:29:08,226
to your computer and
see what was NSLogged.

526
00:29:09,356 --> 00:29:11,486
But that's rare.

527
00:29:11,486 --> 00:29:12,366
Like, anything released

528
00:29:12,366 --> 00:29:13,846
to the App Store isn't
going to have any of those.

529
00:29:14,516 --> 00:29:22,576
[ Pause ]

530
00:29:23,076 --> 00:29:28,216
OK. So this is our first
exposure to an object,

531
00:29:28,216 --> 00:29:28,906
but it doesn't really feel

532
00:29:28,906 --> 00:29:30,306
like an object 'cause
we're pretty much putting

533
00:29:30,306 --> 00:29:31,286
that sign in front of it.

534
00:29:31,686 --> 00:29:35,836
That's technically shorthand
for creating a string object.

535
00:29:36,356 --> 00:29:39,296
And we-- I think we'll
see the longhand later

536
00:29:39,296 --> 00:29:41,126
or at least see many
other objects

537
00:29:41,126 --> 00:29:42,566
that we're going to be creating.

538
00:29:43,276 --> 00:29:45,756
So, use those signs that
just says everything

539
00:29:45,756 --> 00:29:46,916
so far has been the
command line.

540
00:29:47,266 --> 00:29:49,336
Everything can be done
at the command line.

541
00:29:49,936 --> 00:29:52,256
You probably won't want to do
things at the command line.

542
00:29:52,846 --> 00:29:55,906
So, Xcode is pretty--

543
00:29:57,486 --> 00:30:00,636
pretty comprehensive on all
the things Objective-C in iOS.

544
00:30:02,446 --> 00:30:08,966
You should be able to get it for
free and we aren't really going

545
00:30:08,966 --> 00:30:11,766
to go into the details of what
all these different places

546
00:30:11,766 --> 00:30:12,616
in Xcode are.

547
00:30:12,616 --> 00:30:14,526
It's pretty overwhelming
the first time you see it.

548
00:30:14,876 --> 00:30:17,056
The only things we're going
to be using today, like--

549
00:30:17,056 --> 00:30:19,516
it's going to look
like three of them.

550
00:30:19,726 --> 00:30:21,486
So, we're not going to
be using utility area.

551
00:30:22,026 --> 00:30:23,896
The debug area is where
we're going to use to see

552
00:30:23,896 --> 00:30:25,976
where NSLog things are
going to be printing.

553
00:30:26,136 --> 00:30:28,646
The editor area is where
we're going to see our code.

554
00:30:28,996 --> 00:30:34,476
And navigator area, we're
just going to see our files.

555
00:30:34,476 --> 00:30:37,206
And it also contains all this
debuggings, important stuff,

556
00:30:37,206 --> 00:30:39,556
but we're not going
to go into that.

557
00:30:39,736 --> 00:30:46,436
So, you could-- if you have
an iOS device and you want

558
00:30:46,436 --> 00:30:48,506
to actually install these
apps to your iOS device,

559
00:30:48,566 --> 00:30:49,936
this was discussed last week.

560
00:30:50,436 --> 00:30:52,546
But you need a developer
account.

561
00:30:52,896 --> 00:30:55,406
So through us, you can
get a free account.

562
00:30:55,796 --> 00:30:58,736
But you are not able to actually
put the things on the App Store.

563
00:30:58,926 --> 00:31:01,846
So if you want to put things
on the App Store, you will want

564
00:31:01,846 --> 00:31:05,936
to get your own accounts which
is 99 dollars per yourself

565
00:31:05,936 --> 00:31:08,296
and I think like 2.99 for
a company or something.

566
00:31:09,286 --> 00:31:13,626
So, you're going to
look up a lot more there

567
00:31:14,176 --> 00:31:17,046
and this will be
put online later.

568
00:31:18,206 --> 00:31:21,786
So, the new data
types in Objective-C,

569
00:31:22,376 --> 00:31:26,536
there is pretty much only
two big primitive data types

570
00:31:26,536 --> 00:31:29,936
that you need to care
about, so BOOL and ID.

571
00:31:30,466 --> 00:31:33,776
So, a BOOL is like a BOOL
in every other language.

572
00:31:34,226 --> 00:31:37,106
Unfortunately, it is
not true and false.

573
00:31:37,166 --> 00:31:38,536
It is yes and no.

574
00:31:39,096 --> 00:31:40,576
So you're going to be
seeing a lot of capital,

575
00:31:40,576 --> 00:31:42,706
yes's and capital no's
as in your programs.

576
00:31:43,366 --> 00:31:46,376
I think the reason for that
is once we started seeing

577
00:31:46,376 --> 00:31:50,206
functions, the way the
functions are awarded,

578
00:31:50,206 --> 00:31:52,936
it's kind of like--
and do you want this?

579
00:31:53,286 --> 00:31:54,806
Yes. And do you want this?

580
00:31:54,806 --> 00:32:00,996
No. ID. So, if you're
more familiar with C

581
00:32:00,996 --> 00:32:02,326
and you know what void star is,

582
00:32:02,326 --> 00:32:04,716
ID is sort of like
that but for objects.

583
00:32:04,996 --> 00:32:08,826
ID just sort of represents
like a generic object.

584
00:32:08,826 --> 00:32:11,026
You don't know what the type
of the object is, but you know

585
00:32:11,026 --> 00:32:12,416
that this thing is an object.

586
00:32:13,236 --> 00:32:17,146
And nil is sort of the counter
point-- counterpart of null.

587
00:32:17,426 --> 00:32:22,346
So nil represents a
non-existing object

588
00:32:22,526 --> 00:32:23,886
but it has some special
properties.

589
00:32:26,136 --> 00:32:26,816
Questions?

590
00:32:30,166 --> 00:32:34,206
OK. So here are some data
types that if you saw

591
00:32:34,206 --> 00:32:36,676
when we hash imported foundation
slash foundation dot H

592
00:32:37,006 --> 00:32:38,006
which pretty much
everything you're going

593
00:32:38,006 --> 00:32:39,086
to be doing is going to do.

594
00:32:39,086 --> 00:32:42,616
We also get some of these
data types and as integer

595
00:32:42,616 --> 00:32:44,846
and as point and as rect.

596
00:32:45,036 --> 00:32:49,856
These are not objects and
this can be somewhat confusing

597
00:32:50,966 --> 00:32:53,266
because it can be somewhat
difficult to differentiate

598
00:32:53,266 --> 00:32:56,506
between objects and just
these typedef things.

599
00:32:56,946 --> 00:33:04,796
So, they exist for various,
like, legacy reasons and so,

600
00:33:04,796 --> 00:33:08,826
like, NSInteger exist as sort of
this weird bridge between 32 bit

601
00:33:08,826 --> 00:33:12,656
and 64 bit and pretty much
use it whenever like--

602
00:33:12,836 --> 00:33:14,476
when you're looking at
documentation and you see

603
00:33:14,476 --> 00:33:16,756
that this function
returns an NSInteger,

604
00:33:17,286 --> 00:33:18,816
use an NSInteger variable.

605
00:33:19,326 --> 00:33:24,616
So, none of these
are object types.

606
00:33:25,866 --> 00:33:28,576
Now, just a quick
discussion about what classes

607
00:33:28,576 --> 00:33:31,086
and objects are in
case you don't have any

608
00:33:31,086 --> 00:33:32,796
object-oriented experience.

609
00:33:33,426 --> 00:33:36,776
So, we're going to be
defining classes today.

610
00:33:37,286 --> 00:33:41,916
And a class in-- It's going
to be similar in Objective-C

611
00:33:41,916 --> 00:33:43,216
to other languages you've seen.

612
00:33:43,406 --> 00:33:46,756
A class is going to define
methods and variables

613
00:33:47,246 --> 00:33:50,756
and it's pretty much a blueprint
that then objects, you're going

614
00:33:50,756 --> 00:33:52,236
to create from those classes.

615
00:33:52,716 --> 00:33:54,766
So a class is going
to say like any object

616
00:33:54,766 --> 00:33:58,116
of this class should have these
variables and these methods.

617
00:33:58,676 --> 00:34:02,546
And then it's on those objects
that-- or in those objects,

618
00:34:02,546 --> 00:34:04,536
you're actually going
to have those variables.

619
00:34:05,446 --> 00:34:10,066
So, you have a single class
and many, many objects can come

620
00:34:10,106 --> 00:34:11,186
from that single class.

621
00:34:12,186 --> 00:34:13,946
It's also important to remember

622
00:34:13,946 --> 00:34:16,906
that in object-oriented
programming, classes,

623
00:34:17,136 --> 00:34:19,326
you're going to have this
hierarchical relationship

624
00:34:19,326 --> 00:34:20,576
that we're going to be seeing.

625
00:34:20,856 --> 00:34:22,776
So like if I define-- Well,

626
00:34:23,306 --> 00:34:25,736
in Java you might have
seen like the objectClass.

627
00:34:26,046 --> 00:34:29,576
In Objective-C, the
equivalent is NSObject.

628
00:34:30,096 --> 00:34:35,266
So, everything, almost
everything in some way inherits

629
00:34:35,266 --> 00:34:37,056
from this NSObject class.

630
00:34:37,646 --> 00:34:44,736
And so-- so if I defined some
class, person that inherits

631
00:34:44,736 --> 00:34:47,856
from NSObject, then I
might define another class,

632
00:34:47,856 --> 00:34:50,166
student that inherits
from a person

633
00:34:50,166 --> 00:34:52,696
and then I might
define some other class,

634
00:34:52,816 --> 00:34:55,346
8th grader that inherits
from student.

635
00:34:55,666 --> 00:34:58,316
So, remember that these
classes have these

636
00:34:58,316 --> 00:34:59,686
hierarchical relationships.

637
00:35:00,316 --> 00:35:02,606
And this is important
when you're looking

638
00:35:02,606 --> 00:35:05,316
through the documentation
because if I look

639
00:35:05,316 --> 00:35:09,026
at some random-- like, if I
look at the Apple documentation

640
00:35:09,026 --> 00:35:12,406
for the 8th grader class
and it shows me, like,

641
00:35:13,016 --> 00:35:14,516
the method spelling homework.

642
00:35:14,916 --> 00:35:16,736
You might think that's
the only method it has,

643
00:35:17,176 --> 00:35:18,646
but it has a lot more
methods than that

644
00:35:18,646 --> 00:35:21,096
because it's also inherits
all of the methods from all

645
00:35:21,096 --> 00:35:22,036
of his parent classes.

646
00:35:22,636 --> 00:35:25,256
So, you need to look
through the hierarchy

647
00:35:25,256 --> 00:35:27,546
to know all the methods
everything has.

648
00:35:29,236 --> 00:35:33,096
Question? OK.

649
00:35:33,656 --> 00:35:35,296
So, .h files.

650
00:35:37,136 --> 00:35:43,236
Unfortunately, the interface
keyword is somewhat different

651
00:35:43,236 --> 00:35:46,066
from what you might think
of as like a Java interface

652
00:35:46,406 --> 00:35:48,106
and some other languages
interfaces.

653
00:35:48,806 --> 00:35:51,186
It's still similar and that
you're going to be defining

654
00:35:51,546 --> 00:35:54,736
like you're just going to list
the prototypes of the methods

655
00:35:54,736 --> 00:35:57,026
that you want to
be in this class.

656
00:35:58,036 --> 00:36:02,546
But we'll see later this thing
protocols and those are going

657
00:36:02,546 --> 00:36:04,206
to be more like the
interfaces you're used

658
00:36:04,206 --> 00:36:05,116
to in other languages.

659
00:36:05,936 --> 00:36:11,696
So in .h files, we see--
we have this @interface,

660
00:36:12,136 --> 00:36:15,926
and so @ as usual telling
us it's Objective-C.

661
00:36:16,276 --> 00:36:19,726
So @interface and this class
is going to be called foo

662
00:36:19,896 --> 00:36:22,366
and then this colon
indicates the class

663
00:36:22,436 --> 00:36:23,806
that we are inheriting from it.

664
00:36:24,036 --> 00:36:26,116
So our parent is
going to be NSObject.

665
00:36:27,106 --> 00:36:30,846
And then this ugly syntax
where we put curly braces

666
00:36:30,846 --> 00:36:33,116
and then instance variables
go in the curly braces.

667
00:36:33,966 --> 00:36:36,076
And then underneath
the curly braces,

668
00:36:36,076 --> 00:36:39,276
we declare all the methods that
this class needs to define.

669
00:36:39,816 --> 00:36:43,146
And we'll see plenty
examples of those.

670
00:36:43,696 --> 00:36:45,636
And then getting into .m files.

671
00:36:46,056 --> 00:36:48,796
So pretty much every
.m files is going

672
00:36:48,796 --> 00:36:50,696
to have its corresponding
.h file.

673
00:36:50,966 --> 00:36:53,656
Inside of the .m file, we
give the implementation

674
00:36:53,966 --> 00:36:55,016
of these things.

675
00:36:55,016 --> 00:36:57,716
And so here we're going to
give the implementation of foo

676
00:36:57,866 --> 00:36:59,456
and actually define
all the methods

677
00:36:59,456 --> 00:37:01,756
that in this .h file we
said we would define.

678
00:37:05,516 --> 00:37:07,866
OK. So we'll look at an example.

679
00:37:08,326 --> 00:37:13,006
And this time we'll do
it in the next code.

680
00:37:13,116 --> 00:37:13,726
Actually.

681
00:37:14,516 --> 00:37:24,816
[ Pause ]

682
00:37:25,316 --> 00:37:25,836
All right.

683
00:37:26,636 --> 00:37:31,526
So, here we see on this left
side, we have our files.

684
00:37:31,856 --> 00:37:33,606
I'm going to get rid of this
right side 'cause we don't need

685
00:37:33,606 --> 00:37:33,996
it now.

686
00:37:34,376 --> 00:37:36,176
Up in the-- or down
at the bottom,

687
00:37:36,296 --> 00:37:38,816
we're going to have our
NSLogs stuff down here.

688
00:37:39,526 --> 00:37:41,276
So looking through these files--

689
00:37:41,556 --> 00:37:44,596
I'm not going to
be able to do that.

690
00:37:45,076 --> 00:37:50,986
So looking to these files and we
see-- we have a main function.

691
00:37:51,196 --> 00:37:53,056
We have the same
AutoReleasePool thing

692
00:37:53,056 --> 00:37:54,356
that you can ignore for now.

693
00:37:54,856 --> 00:37:57,646
And then we're going to have
these student star Alice.

694
00:37:58,076 --> 00:38:03,256
So, student we see over here
is going to be an object.

695
00:38:03,256 --> 00:38:05,226
We have .h file and
.m file for it.

696
00:38:06,676 --> 00:38:09,416
We'll look at those in a second.

697
00:38:09,416 --> 00:38:14,856
Student star, so I
guess I'll go into that.

698
00:38:16,096 --> 00:38:17,616
Ignore this for a second.

699
00:38:18,296 --> 00:38:20,316
So, just student star Alice,

700
00:38:20,656 --> 00:38:22,016
we're going to allocate
the student.

701
00:38:22,176 --> 00:38:23,716
So this is sort of like malloc.

702
00:38:24,026 --> 00:38:26,706
So, we're making
broom for the student.

703
00:38:27,386 --> 00:38:30,716
And then apparently,
we have some age field

704
00:38:30,716 --> 00:38:31,736
and some name field.

705
00:38:31,736 --> 00:38:33,066
Let's actually look at it.

706
00:38:33,066 --> 00:38:36,086
So here's student.h. And
we have interface student,

707
00:38:36,086 --> 00:38:37,896
it's inheriting from NSObject.

708
00:38:38,756 --> 00:38:41,336
And here we see this
@public keyword.

709
00:38:41,716 --> 00:38:44,296
This tells us that
everything underneath that--

710
00:38:44,366 --> 00:38:46,376
all the variables
underneath this word are going

711
00:38:46,376 --> 00:38:48,006
to be public instance variables.

712
00:38:48,536 --> 00:38:51,016
There's also app
protected and app private.

713
00:38:51,236 --> 00:38:53,536
So private, no other
class is going to be able

714
00:38:53,536 --> 00:38:57,466
to see these variables
and protected any class

715
00:38:57,516 --> 00:38:59,896
that is a subclass of this
class is going to be able

716
00:38:59,896 --> 00:39:01,076
to access these variables.

717
00:39:01,906 --> 00:39:04,606
So you won't need to
deal with those too much.

718
00:39:05,536 --> 00:39:08,076
Generally, you want
to make the access

719
00:39:08,076 --> 00:39:10,206
to these variables
as tight as possible.

720
00:39:10,306 --> 00:39:13,546
So like if the variables can
be private, make them private.

721
00:39:14,476 --> 00:39:19,286
So here, the instance variables
we have are age and name

722
00:39:19,286 --> 00:39:20,986
which is an NSStrings star.

723
00:39:21,516 --> 00:39:25,966
And we see since there are
no methods defined down here,

724
00:39:26,136 --> 00:39:27,926
that means that, well,

725
00:39:27,926 --> 00:39:32,836
let's look at it student.m
doesn't need define any methods.

726
00:39:34,136 --> 00:39:34,456
Yes?

727
00:39:35,516 --> 00:39:38,776
[ Inaudible Remark ]

728
00:39:39,276 --> 00:39:40,926
Why do we use what?

729
00:39:41,516 --> 00:39:44,636
[ Inaudible Remark ]

730
00:39:45,136 --> 00:39:50,456
Oh, so, we could use
string or just char star

731
00:39:50,456 --> 00:39:54,796
like a regular C type string,
but in Objective-C, you're going

732
00:39:54,796 --> 00:39:57,626
to be passing around
these NSStrings a lot.

733
00:39:57,626 --> 00:39:59,976
So you're probably going to want
this thing to be an NSString.

734
00:40:00,096 --> 00:40:02,406
Like when we want to
actually print this,

735
00:40:02,546 --> 00:40:04,246
we're going to need
print an NSString.

736
00:40:04,246 --> 00:40:06,796
We can't-- Oh, when we
want to NSLog something,

737
00:40:06,796 --> 00:40:08,266
we need to NSLog objects.

738
00:40:08,666 --> 00:40:11,076
So we're going to need
to pass it in NSString.

739
00:40:11,076 --> 00:40:13,256
We can't pass it a char star.

740
00:40:13,326 --> 00:40:15,916
We would have to change that
char star to an NSString.

741
00:40:16,046 --> 00:40:18,566
>> But we also got
NSInteger [inaudible].

742
00:40:18,566 --> 00:40:23,466
>> Yeah. So, NSInteger
is-- this is where it's--

743
00:40:23,856 --> 00:40:29,446
NSInteger is pretty much
going to be just a typedef

744
00:40:29,496 --> 00:40:32,266
for regular int or
possible long.

745
00:40:32,506 --> 00:40:35,276
This is not an object type.

746
00:40:35,786 --> 00:40:36,826
This is not a class.

747
00:40:37,056 --> 00:40:42,396
This is just like
typedef, NSInteger, int.

748
00:40:45,736 --> 00:40:51,556
So, this why like-- these aren't
that helpful unless you see

749
00:40:51,556 --> 00:40:52,256
that a function

750
00:40:52,256 --> 00:40:54,726
in the documentation is
returning an NSInteger.

751
00:40:55,006 --> 00:40:56,976
And then you should
use NSIntegers.

752
00:40:57,516 --> 00:41:04,436
[ Inaudible Remark ]

753
00:41:04,936 --> 00:41:08,046
Yes. So that's the
unfortunate thing.

754
00:41:08,396 --> 00:41:14,516
You got pretty-- like the
primitive ones are these NS--

755
00:41:14,516 --> 00:41:16,616
well, actually no,
I can even say that.

756
00:41:16,616 --> 00:41:18,546
There are some complicated
ones that are primitive

757
00:41:18,546 --> 00:41:21,426
and I wish they had
done something

758
00:41:21,426 --> 00:41:23,666
to differentiate
better, but they didn't.

759
00:41:23,666 --> 00:41:29,716
and let's change back to NS, but
it shouldn't change anything.

760
00:41:30,846 --> 00:41:35,616
So, it mean-- So we
are now accessing this.

761
00:41:35,836 --> 00:41:37,756
So student star Alice
is an object.

762
00:41:38,076 --> 00:41:41,426
That object has an age
field and a name field

763
00:41:41,716 --> 00:41:44,876
and we are accessing those and
setting the age field to 20

764
00:41:44,876 --> 00:41:51,216
and we're setting the name
field to be this NSString Alice.

765
00:41:51,726 --> 00:41:55,806
Note that this would not
succeed without the @

766
00:41:55,806 --> 00:41:57,496
because now it's not an NSString

767
00:41:57,496 --> 00:41:58,876
and it yells at us
appropriately.

768
00:42:01,276 --> 00:42:08,546
And in case you're curious,
this arrow syntax, it comes more

769
00:42:08,546 --> 00:42:12,156
from C, come-- the next
example that I gave you,

770
00:42:12,156 --> 00:42:14,316
you will not be using
this arrow syntax.

771
00:42:14,846 --> 00:42:19,006
But what it is, is-- so,

772
00:42:19,386 --> 00:42:22,106
we're treating Alice
sort of as like a struct.

773
00:42:22,816 --> 00:42:25,386
And so, we are de-referencing
that struct

774
00:42:26,026 --> 00:42:30,756
and then accessing the age
field inside of that struck.

775
00:42:31,346 --> 00:42:36,506
So, arrow is equivalent
to star Alice dot.

776
00:42:37,076 --> 00:42:40,856
It's-- This to just say very
disgusting syntax since,

777
00:42:40,856 --> 00:42:41,846
that's why they give us arrow.

778
00:42:42,316 --> 00:42:47,796
Arrow just means, here is a
pointer to a struct/object

779
00:42:48,546 --> 00:42:52,836
and go to that object and change
this field at that object.

780
00:42:57,736 --> 00:43:02,186
But you will not be using
arrows on objects ever.

781
00:43:02,626 --> 00:43:05,146
So, this is a pretty
bad example,

782
00:43:05,216 --> 00:43:06,496
but this is our intro example.

783
00:43:08,116 --> 00:43:09,706
Do the same thing
with Bob, we allocate

784
00:43:09,706 --> 00:43:11,876
and we give him an age,
we give him a name.

785
00:43:12,166 --> 00:43:14,486
And then greet [phonetic]
down here.

786
00:43:14,656 --> 00:43:21,726
We're going to NSLog and we see
NSLog and we need to pass NSLog

787
00:43:21,726 --> 00:43:23,796
and NSString as its
first argument.

788
00:43:24,146 --> 00:43:29,306
And so we see our normal percent
D over here and as age is just

789
00:43:29,306 --> 00:43:32,936
as we saw in the student.h
file, as age is just an int.

790
00:43:33,406 --> 00:43:34,346
And so that's fine.

791
00:43:34,936 --> 00:43:38,876
And as name was an NSString.

792
00:43:39,286 --> 00:43:43,286
And so, the NSLog
version of like percent S

793
00:43:43,516 --> 00:43:48,796
for NSStrings is going
to be this percent @. So,

794
00:43:48,796 --> 00:43:52,286
that is the flag for
printing NSStrings.

795
00:43:54,136 --> 00:43:57,916
Actually running this, on
top, hitting build or run.

796
00:43:58,046 --> 00:43:59,846
I forgot the shortcut for it.

797
00:43:59,846 --> 00:44:03,106
And down here we see
the NSLog output.

798
00:44:03,266 --> 00:44:04,476
Hello Alice, you're 20.

799
00:44:04,636 --> 00:44:05,886
Hi Bob, you're 21.

800
00:44:09,316 --> 00:44:12,936
Questions on this simple
example because it's going

801
00:44:12,936 --> 00:44:14,716
to be changing pretty
substantially

802
00:44:14,716 --> 00:44:15,736
in the next example?

803
00:44:18,326 --> 00:44:20,326
[ Inaudible Remark ]

804
00:44:20,636 --> 00:44:24,976
Yeah. So, this is also
what I kind of pause

805
00:44:24,976 --> 00:44:26,666
at before 'cause I don't
want to say quite yet.

806
00:44:27,306 --> 00:44:30,576
Student star, student
is really like--

807
00:44:30,576 --> 00:44:36,066
Alice is the object but like
pretty much read student star--

808
00:44:36,066 --> 00:44:38,216
don't read that as a
pointer to a student,

809
00:44:38,446 --> 00:44:42,176
just take student star to be
like the type of the object.

810
00:44:42,586 --> 00:44:47,036
So, you will never really be
dealing with a student Alice.

811
00:44:47,796 --> 00:44:50,416
You will always be dealing
with a student star Alice.

812
00:44:54,216 --> 00:45:00,976
And in the next sample,
going to students to--

813
00:45:01,516 --> 00:45:06,516
[ Pause ]

814
00:45:07,016 --> 00:45:15,000
[ Inaudible Remark ]

815
00:45:15,896 --> 00:45:16,886
No, not really.

816
00:45:17,516 --> 00:45:21,736
[ Pause ]

817
00:45:22,236 --> 00:45:25,866
OK. So, in this example,
let's look at .h first.

818
00:45:27,736 --> 00:45:33,026
So now, this is the same,
it's inheriting from NSObject.

819
00:45:33,456 --> 00:45:35,196
We have changed this.

820
00:45:35,526 --> 00:45:38,106
We're no longer saying
int age and NSString name.

821
00:45:38,176 --> 00:45:40,826
We're saying underscore
age and underscore name.

822
00:45:41,106 --> 00:45:45,146
And that is a convention
that in the past it used just

823
00:45:45,146 --> 00:45:46,876
to be a convention and
now it's a convention

824
00:45:46,876 --> 00:45:48,056
that actually mean something.

825
00:45:48,056 --> 00:45:48,976
So you're going to want

826
00:45:48,976 --> 00:45:51,066
to pretty much always have
these instance variables

827
00:45:51,066 --> 00:45:52,006
be underscored.

828
00:45:52,756 --> 00:45:55,166
And now, here we're getting

829
00:45:55,166 --> 00:45:58,526
to these strange
looking method things.

830
00:45:58,526 --> 00:46:00,976
So, let's actually go back
to slides for a second.

831
00:46:01,516 --> 00:46:03,516
[ Pause ]

832
00:46:04,016 --> 00:46:06,000
[ Inaudible Remark ]

833
00:46:06,616 --> 00:46:11,236
We'll see in a second why
we want the underscores

834
00:46:12,226 --> 00:46:13,546
and I quite gotten there yet.

835
00:46:14,666 --> 00:46:16,246
OK. So instance variables,
we saw,

836
00:46:16,246 --> 00:46:18,386
we know them @protected,
@private, @public.

837
00:46:18,846 --> 00:46:20,216
And it's somewhat messy.

838
00:46:20,216 --> 00:46:21,816
But like when I said @public,

839
00:46:21,816 --> 00:46:23,876
everything beneath the
@public would be public.

840
00:46:24,196 --> 00:46:26,946
But then if I switch to--
like if I @public int X

841
00:46:27,056 --> 00:46:30,226
and @private int Y. Y would be
private 'cause it's underneath

842
00:46:30,566 --> 00:46:31,706
the private.

843
00:46:32,586 --> 00:46:35,926
OK. So, first let's
look at this.

844
00:46:36,446 --> 00:46:38,536
So, messages.

845
00:46:39,156 --> 00:46:41,436
In other object-orientated
languages,

846
00:46:41,436 --> 00:46:43,496
you don't really--
well, some you do.

847
00:46:43,646 --> 00:46:44,856
But Java, you don't.

848
00:46:45,656 --> 00:46:49,256
So, messages are going to be
the way that we invoke methods

849
00:46:49,346 --> 00:46:51,366
on objects and classes.

850
00:46:52,076 --> 00:46:55,726
So here, this square bracket
syntax is what you're going

851
00:46:55,726 --> 00:46:57,206
to be using to send messages.

852
00:46:57,206 --> 00:46:58,856
And here we're saying,

853
00:46:59,266 --> 00:47:03,026
the student class has a
method called "alloc."

854
00:47:03,596 --> 00:47:07,476
And so, send to the
student class that method.

855
00:47:08,016 --> 00:47:09,306
Invoke that method.

856
00:47:09,856 --> 00:47:12,886
And then that is supposed
to return a student star.

857
00:47:13,816 --> 00:47:19,706
And so this alloc is going
to be pretty much everywhere.

858
00:47:19,866 --> 00:47:22,816
You-- Whenever you need
to allocate an object,

859
00:47:22,816 --> 00:47:24,256
you need to do some
sort of alloc.

860
00:47:24,876 --> 00:47:27,666
Sometimes, there is like alloc,

861
00:47:29,456 --> 00:47:32,106
sometimes there are some other
alloc type function you might be

862
00:47:32,106 --> 00:47:34,126
using, but you're
almost always going

863
00:47:34,126 --> 00:47:35,686
to be alloc'ing these things.

864
00:47:36,716 --> 00:47:41,436
So, here-- So there are two
different types of methods

865
00:47:41,546 --> 00:47:43,676
like there are in other
object-oriented languages.

866
00:47:44,036 --> 00:47:46,526
So we have instance
methods and class methods.

867
00:47:47,156 --> 00:47:48,596
And so, the way we're
going to differentiate

868
00:47:48,596 --> 00:47:51,306
between those is all the
way on the left side,

869
00:47:51,306 --> 00:47:52,626
you see we have this minus sign.

870
00:47:53,646 --> 00:47:56,906
So, if we have a minus sign
that means the method is going

871
00:47:56,906 --> 00:47:58,046
to be an instance method.

872
00:47:58,046 --> 00:48:00,326
And if we have a
plus sign that means

873
00:48:00,326 --> 00:48:02,126
that it's going to
be a class method.

874
00:48:02,746 --> 00:48:04,516
And an instance method
is something

875
00:48:04,516 --> 00:48:07,516
that you actually need an
object to use the method

876
00:48:07,836 --> 00:48:09,036
and a class method is something

877
00:48:09,036 --> 00:48:10,946
where you don't need an
object to use the method.

878
00:48:11,096 --> 00:48:14,136
So going back to here, the
fact that we're calling alloc

879
00:48:14,136 --> 00:48:18,076
on the student class tells us
that alloc is a class method

880
00:48:18,576 --> 00:48:23,076
because we don't actually need
a student to allocate a student.

881
00:48:23,436 --> 00:48:25,906
That'd be pretty flawed if we
couldn't actually get a student

882
00:48:25,906 --> 00:48:27,876
without having a student.

883
00:48:28,516 --> 00:48:30,796
[ Inaudible Remark ]

884
00:48:31,296 --> 00:48:32,416
Yes. So, yeah.

885
00:48:32,656 --> 00:48:36,336
The static keyword from Java
is the equivalent of plus.

886
00:48:36,856 --> 00:48:44,116
So the syntax for these
things, we see all the way

887
00:48:44,196 --> 00:48:47,926
in the left is going to
go plus or minus always.

888
00:48:47,926 --> 00:48:51,006
And then we're going to have
the return type of the method.

889
00:48:51,296 --> 00:48:54,146
So here we have void, int, void.

890
00:48:54,946 --> 00:48:58,066
Then after return type, we
have the name of the method.

891
00:48:58,616 --> 00:49:03,536
And so we have the name
init and we the name age.

892
00:49:03,866 --> 00:49:05,186
And then technically the name

893
00:49:05,186 --> 00:49:08,816
of the last method is
going to be set age colon.

894
00:49:09,816 --> 00:49:11,516
We'll see why.

895
00:49:11,936 --> 00:49:18,196
But this here-- this age
is what we're just going

896
00:49:18,196 --> 00:49:19,676
to call that variable.

897
00:49:19,896 --> 00:49:21,506
And this is the type
of that variable.

898
00:49:22,066 --> 00:49:25,106
So, this method isn't
going to return anything.

899
00:49:25,456 --> 00:49:28,486
It's called set age colon and
we would pass it some integer.

900
00:49:29,516 --> 00:49:33,436
[ Pause ]

901
00:49:33,936 --> 00:49:38,236
So, sending those messages,
we see that student--

902
00:49:38,236 --> 00:49:41,316
we're using lowercase student,
so the student over here,

903
00:49:41,316 --> 00:49:44,166
the student object, since
these are instance methods,

904
00:49:44,166 --> 00:49:46,736
we need to send these
messages to objects.

905
00:49:47,866 --> 00:49:50,576
So we send the init
message to student.

906
00:49:51,006 --> 00:49:52,896
We send the age message
to student.

907
00:49:52,896 --> 00:49:54,466
And then we sent set age

908
00:49:54,746 --> 00:49:58,116
with the parameter 20,
with the argument 20.

909
00:49:59,596 --> 00:50:00,406
Questions?

910
00:50:02,516 --> 00:50:13,176
[ Inaudible Remark ]

911
00:50:13,676 --> 00:50:14,706
What do you mean?

912
00:50:15,516 --> 00:50:21,236
[ Inaudible Remark ]

913
00:50:21,736 --> 00:50:24,856
>> Yes. That is how
you should call it.

914
00:50:25,516 --> 00:50:28,196
[ Inaudible Remark ]

915
00:50:28,696 --> 00:50:29,726
Could you call as what?

916
00:50:29,726 --> 00:50:33,326
>> Like I would in C.

917
00:50:33,576 --> 00:50:35,116
>> How would you call it in C?

918
00:50:35,116 --> 00:50:41,826
>> I mean is that like I would
say C and then like that?

919
00:50:41,826 --> 00:50:42,336
>> Oh err-- so no.

920
00:50:42,886 --> 00:50:45,876
This is why the previous example
is something that was bad.

921
00:50:45,876 --> 00:50:49,566
You should very barely
be differencing objects.

922
00:50:49,856 --> 00:50:52,106
I almost want to say
never difference objects.

923
00:50:52,106 --> 00:50:55,566
And so, the arrow operator
is n implicit difference.

924
00:50:56,036 --> 00:50:58,396
And so init is also
somewhat different

925
00:50:58,396 --> 00:51:00,926
from setting the
fields of the object.

926
00:51:01,486 --> 00:51:04,836
And another reason why the
previous example was bad,

927
00:51:05,516 --> 00:51:09,006
you always-- you never
want to deal with an object

928
00:51:09,006 --> 00:51:11,016
that was allocated
but not initialized.

929
00:51:11,496 --> 00:51:15,826
So, even though the student
objects, and we'll see

930
00:51:15,826 --> 00:51:17,076
in the next example how--

931
00:51:17,176 --> 00:51:19,346
how originally just
had like student alloc

932
00:51:19,456 --> 00:51:21,156
and I did Alice arrow age.

933
00:51:21,876 --> 00:51:23,106
I shouldn't have done that.

934
00:51:23,106 --> 00:51:25,976
I should have done
student alloc init.

935
00:51:26,716 --> 00:51:28,606
So always initialize
your objects

936
00:51:29,036 --> 00:51:30,736
and then I could
access those fields.

937
00:51:31,436 --> 00:51:32,436
But then on top of that,

938
00:51:32,436 --> 00:51:34,426
I shouldn't be accessing
those fields using arrows,

939
00:51:34,426 --> 00:51:35,496
I should be sending messages.

940
00:51:36,086 --> 00:51:39,296
And so we'll see that
in the next example,

941
00:51:39,296 --> 00:51:40,746
the right way of doing that.

942
00:51:40,746 --> 00:51:43,726
And then we'll see that
there are like 10 new

943
00:51:43,726 --> 00:51:45,826
or right ways of doing it.

944
00:51:48,256 --> 00:51:54,956
So selectors-- pretty much an
alternative name for method,

945
00:51:55,196 --> 00:51:57,676
so alloc, init age
and setAge colon.

946
00:51:57,676 --> 00:51:59,366
Remember the colon
is part of the name?

947
00:51:59,686 --> 00:52:02,006
Once we see multiple
argument functions,

948
00:52:02,006 --> 00:52:04,566
we'll see that there will be
multiple callings in the name.

949
00:52:05,556 --> 00:52:08,396
So, all of those
are just selectors

950
00:52:08,396 --> 00:52:10,206
and we'll see why
that's relevant later.

951
00:52:11,176 --> 00:52:15,636
And let's look at
that example now.

952
00:52:18,116 --> 00:52:20,166
So student two, OK.

953
00:52:20,806 --> 00:52:24,626
So, here we have said
int underscore age

954
00:52:25,606 --> 00:52:27,126
and underscore name.

955
00:52:27,646 --> 00:52:32,756
And now we have for methods
and this is like the getters

956
00:52:32,756 --> 00:52:34,946
and setters of Java that
you might be used to.

957
00:52:34,946 --> 00:52:40,416
We have two methods, one called
age and another called setAge

958
00:52:42,006 --> 00:52:45,526
that the first one is going
to get the age of the thing.

959
00:52:45,776 --> 00:52:48,376
And the second one we're
going to use to set the age.

960
00:52:48,506 --> 00:52:51,376
And then similarly down here,
we have name and setName,

961
00:52:51,376 --> 00:52:53,566
the first we're going to
use to retrieve the name

962
00:52:53,816 --> 00:52:56,276
and the second we're going
to use to set the name.

963
00:52:56,766 --> 00:53:01,196
And these are important
names both by convention

964
00:53:01,196 --> 00:53:03,006
and we'll see things
that help us later

965
00:53:03,276 --> 00:53:04,956
where in other languages
you might say

966
00:53:04,956 --> 00:53:06,296
like getName and setName.

967
00:53:06,546 --> 00:53:08,766
Here, the convention
is just to say name.

968
00:53:08,956 --> 00:53:10,046
Do not say getName.

969
00:53:11,916 --> 00:53:12,416
Questions?

970
00:53:12,546 --> 00:53:19,536
OK, so looking at
the implementation,

971
00:53:19,696 --> 00:53:21,566
we must be implementing
these for methods.

972
00:53:22,566 --> 00:53:26,976
And so, notice, they are
all instance methods,

973
00:53:26,976 --> 00:53:27,816
they are all minus.

974
00:53:28,776 --> 00:53:32,226
And age, we'll just going
to return underscore age.

975
00:53:33,096 --> 00:53:36,346
And setAge, we're going
to set underscore age.

976
00:53:37,166 --> 00:53:39,346
So we're going to set
our instance variable age

977
00:53:39,616 --> 00:53:41,526
to the pass in argument age.

978
00:53:42,336 --> 00:53:44,806
We're going to return underscore
name and we're going to set,

979
00:53:44,806 --> 00:53:46,976
and this one is somewhat
interesting.

980
00:53:47,236 --> 00:53:50,046
So, we're going to set our
instance variable underscore

981
00:53:50,046 --> 00:53:53,106
name to name copy.

982
00:53:53,946 --> 00:53:57,106
So, we know that this in
fact is how we're going

983
00:53:57,106 --> 00:53:58,686
to send message to objects.

984
00:53:59,666 --> 00:54:02,276
Name is in NSString object.

985
00:54:03,156 --> 00:54:05,876
And so, apparently and
you can look it up--

986
00:54:05,916 --> 00:54:08,536
you can look in the
documentation to verify this.

987
00:54:08,986 --> 00:54:14,746
Apparently name or NSString has
an instance method called Copy

988
00:54:15,486 --> 00:54:18,826
that is going to return
a copy of this NSString.

989
00:54:19,566 --> 00:54:22,216
And why do you think we want
a copy of the NSString instead

990
00:54:22,216 --> 00:54:26,756
of just saying underscore
name equals name?

991
00:54:27,516 --> 00:54:34,616
[ Pause ]

992
00:54:35,116 --> 00:54:35,216
Yeah.

993
00:54:35,396 --> 00:54:42,066
>> Well because it's an object,

994
00:54:42,316 --> 00:54:47,566
it should be modifying the
pointers of that object

995
00:54:47,566 --> 00:54:49,586
so it would be modifying
the initial objects.

996
00:54:49,626 --> 00:54:51,486
>> Yeah, so it isn't--
well actually, so,

997
00:54:51,486 --> 00:54:53,156
if we did not make a copy of
the name, it isn't a thread

998
00:54:53,156 --> 00:54:54,566
that we are going to
modify that NSString

999
00:54:54,796 --> 00:54:57,496
since we don't have any methods
for modifying that NSString.

1000
00:54:57,496 --> 00:55:01,436
I guess we-- if we went around
the proper method protocol,

1001
00:55:01,436 --> 00:55:03,066
we would be doing things.

1002
00:55:03,066 --> 00:55:06,726
We'll see why we should
never be modifying name.

1003
00:55:07,396 --> 00:55:11,136
But it's possible that the
NSString that was passed

1004
00:55:11,136 --> 00:55:14,186
to us @Bob or something
is that--

1005
00:55:14,486 --> 00:55:15,866
that could later be changed.

1006
00:55:16,186 --> 00:55:20,386
And so if that is changed
then if we just do this,

1007
00:55:21,906 --> 00:55:23,006
then we are also going to--

1008
00:55:23,006 --> 00:55:25,306
the name within this object
will have been changed.

1009
00:55:25,476 --> 00:55:27,916
So we want to copy Bob
for ourselves in case

1010
00:55:27,916 --> 00:55:29,976
if Bob has ever changed, we
don't want to be changed.

1011
00:55:30,516 --> 00:55:33,586
[ Pause ]

1012
00:55:34,086 --> 00:55:37,456
OK, so looking at
main.m [phonetic],

1013
00:55:38,166 --> 00:55:40,726
now this is the better
way of doing this.

1014
00:55:41,006 --> 00:55:43,456
This is like appropriate
objective-C.

1015
00:55:43,616 --> 00:55:46,216
We can see that there are more
and more better ways but this is

1016
00:55:46,216 --> 00:55:49,766
at least like not bad.

1017
00:55:49,766 --> 00:55:53,376
So, we are appropriately
initting and notice

1018
00:55:53,376 --> 00:55:56,396
that we are allowed to
like nests these things.

1019
00:55:56,946 --> 00:56:05,506
So, we could if we
wanted do student alloc

1020
00:56:05,766 --> 00:56:09,916
and then Alice equals
Alice init,

1021
00:56:11,536 --> 00:56:14,876
but we are allowed
to nests these calls.

1022
00:56:18,556 --> 00:56:22,476
OK and now we are, instead

1023
00:56:22,476 --> 00:56:28,396
of directly doing Alice arrow
age equals 20 which is bad.

1024
00:56:29,606 --> 00:56:31,856
We are saying Alice
setAge to 20.

1025
00:56:31,856 --> 00:56:35,386
We are sending the message
setAge with the argument in 20

1026
00:56:35,386 --> 00:56:38,226
to Alice and we're
setting the name to Alice

1027
00:56:38,926 --> 00:56:40,186
and similarly with Bob.

1028
00:56:40,306 --> 00:56:41,286
And then what greet
[phonetic] is going

1029
00:56:41,286 --> 00:56:44,946
to do is use this
S name and S age.

1030
00:56:44,946 --> 00:56:49,436
We're going to send those getter
methods to get getter messages

1031
00:56:49,826 --> 00:56:52,136
to get the appropriate
instance variables.

1032
00:56:52,136 --> 00:56:52,203
Yeah.

1033
00:56:53,516 --> 00:56:59,956
[ Inaudible Remark ]

1034
00:57:00,456 --> 00:57:03,366
Oh so, the only reason
we can do that here is

1035
00:57:03,366 --> 00:57:06,766
because student alloc happens
to return the student object.

1036
00:57:07,196 --> 00:57:10,366
And so we are then using that
student object and initting it.

1037
00:57:10,686 --> 00:57:13,336
And that also returns the
initialized student object.

1038
00:57:13,876 --> 00:57:17,466
SetAge doesn't return
this student object.

1039
00:57:17,966 --> 00:57:21,076
If we do we could actually do
that we can do like instead

1040
00:57:21,076 --> 00:57:24,386
of void, we can have it
return the student object.

1041
00:57:25,516 --> 00:57:31,556
[ Pause ]

1042
00:57:32,056 --> 00:57:34,976
>> And then this is going
to get into return self.

1043
00:57:34,976 --> 00:57:36,416
Self is going to be
your keyword here.

1044
00:57:36,416 --> 00:57:36,976
We'll see it later.

1045
00:57:37,516 --> 00:57:42,516
[ Pause ]

1046
00:57:43,016 --> 00:57:52,000
[ Inaudible Remark ]

1047
00:57:52,746 --> 00:57:56,976
So here, are you saying
why should we need--

1048
00:57:57,516 --> 00:58:02,176
[ Inaudible Remark ]

1049
00:58:02,676 --> 00:58:05,996
So this is that you
never pass but--

1050
00:58:05,996 --> 00:58:07,436
you never pass objects by value.

1051
00:58:07,586 --> 00:58:09,816
You never handle objects
as explicit values.

1052
00:58:10,066 --> 00:58:12,606
You're always going to go
through a pointer to the object

1053
00:58:13,006 --> 00:58:14,126
which is why I say pretty--

1054
00:58:14,126 --> 00:58:15,926
think of like NSString star

1055
00:58:15,926 --> 00:58:18,146
as the object not a
pointer to the object.

1056
00:58:18,146 --> 00:58:22,176
Think of it as the object.

1057
00:58:22,176 --> 00:58:24,696
Except that if you modify it,
then it's going to be modified

1058
00:58:24,806 --> 00:58:25,656
because it's a reference.

1059
00:58:26,326 --> 00:58:29,766
OK, so now we're returning
self which is going

1060
00:58:29,766 --> 00:58:33,376
to allow us to do that nested.

1061
00:58:33,556 --> 00:58:40,956
Now we're free to do Alice
setAge 20, setName Alice.

1062
00:58:41,516 --> 00:58:44,516
[ Pause ]

1063
00:58:45,016 --> 00:58:48,000
[ Inaudible Remark ]

1064
00:58:48,996 --> 00:58:50,416
We-- not yet.

1065
00:58:51,776 --> 00:58:54,286
The next example
we'll be able to.

1066
00:58:55,906 --> 00:58:58,506
Just to point out one other
thing that you might see

1067
00:58:58,506 --> 00:59:01,616
in like documentation or
like example online or stuff.

1068
00:59:01,886 --> 00:59:06,896
So alloc init is such an
incredibly common thing

1069
00:59:06,896 --> 00:59:09,166
like you alloc init--
you might be doing some

1070
00:59:09,166 --> 00:59:10,176
different initializer.

1071
00:59:10,416 --> 00:59:14,376
There are different initializers
but using alloc and init is

1072
00:59:14,376 --> 00:59:16,856
so common that there's
actually a shortcut--

1073
00:59:17,516 --> 00:59:20,886
[ Pause ]

1074
00:59:21,386 --> 00:59:25,896
-- New, and so, new
is going to sort of--

1075
00:59:25,896 --> 00:59:30,526
or new is going to first
allocate and then initialize.

1076
00:59:30,526 --> 00:59:33,426
So that does it in one step.

1077
00:59:34,236 --> 00:59:37,516
There are sort of--
well, this is the first

1078
00:59:37,516 --> 00:59:41,276
of three religious wars you'll
hear today about objective-C

1079
00:59:41,526 --> 00:59:42,506
that there are a lot of people

1080
00:59:42,506 --> 00:59:45,036
who will say new is a perfectly
fine thing, and there are lot

1081
00:59:45,036 --> 00:59:47,236
of people who will say you
should absolutely never say new.

1082
00:59:47,426 --> 00:59:50,966
So, we don't care which
you use, whether you want

1083
00:59:50,966 --> 00:59:53,426
to explicitly allocate and
initialize or whatever.

1084
00:59:54,226 --> 00:59:56,226
[ Pause ]

1085
00:59:56,436 --> 00:59:59,506
OK? Questions on any of that?

1086
01:00:00,586 --> 01:00:00,926
Yes?

1087
01:00:01,226 --> 01:00:09,346
>> So does allocate initially
reserve space on the key

1088
01:00:09,346 --> 01:00:11,196
for the object and then setting

1089
01:00:11,196 --> 01:00:12,976
on the instance variables
on that?

1090
01:00:12,976 --> 01:00:14,456
>> Yeah, it is--
potentially other things

1091
01:00:14,456 --> 01:00:16,086
than setting instance variables.

1092
01:00:16,086 --> 01:00:19,166
If you wanted, you're going to
find the initialization method

1093
01:00:19,166 --> 01:00:23,936
that like counts the number
of objects of this type

1094
01:00:23,966 --> 01:00:24,596
that had been initialized.

1095
01:00:24,626 --> 01:00:25,976
But you should never handle
an uninitialized object.

1096
01:00:26,516 --> 01:00:29,736
[ Pause ]

1097
01:00:30,236 --> 01:00:36,656
OK? So then this is going
to be the next example

1098
01:00:36,656 --> 01:00:37,826
that we're going to want to use

1099
01:00:37,826 --> 01:00:41,366
but let's just look
at the slide first.

1100
01:00:41,366 --> 01:00:46,416
So, Property so-- and this is
going to be what we're going

1101
01:00:46,416 --> 01:00:49,236
to use to enable
us to use .syntax.

1102
01:00:49,966 --> 01:00:55,166
We can somewhat ignore a lot
of these keywords for now.

1103
01:00:55,376 --> 01:00:57,866
These assign, copy, strong,
weak are things we're going

1104
01:00:57,866 --> 01:00:58,856
to deal with next week.

1105
01:00:59,686 --> 01:01:04,766
Strong and weak in particular
are pretty much the only things

1106
01:01:04,766 --> 01:01:07,436
you really need to
think about under ARC.

1107
01:01:08,426 --> 01:01:11,726
Those are memory
management difficulties

1108
01:01:11,726 --> 01:01:13,996
that if you got those wrong,
you can still leak things.

1109
01:01:15,936 --> 01:01:18,406
But it's not too bad.

1110
01:01:18,406 --> 01:01:19,536
It's especially better
than needing

1111
01:01:19,536 --> 01:01:20,326
to keep track of everything.

1112
01:01:21,186 --> 01:01:23,856
Atomic and nonatomic are
a different pair of things

1113
01:01:23,856 --> 01:01:25,466
that we'll get into and
we don't mean readwrite.

1114
01:01:25,796 --> 01:01:27,356
We will talk about today.

1115
01:01:27,646 --> 01:01:29,926
So @property, new thing.

1116
01:01:30,736 --> 01:01:31,826
So look at the example.

1117
01:01:32,516 --> 01:01:39,636
[ Pause ]

1118
01:01:40,136 --> 01:01:40,886
Student-free.

1119
01:01:41,516 --> 01:01:44,896
[ Pause ]

1120
01:01:45,396 --> 01:01:51,026
OK. So looking at student.h, the
only thing that is changed is

1121
01:01:51,656 --> 01:01:53,406
that we have added
these two lines.

1122
01:01:54,236 --> 01:01:59,176
So, we have SetAppProperty
and from each of those groups

1123
01:01:59,176 --> 01:02:03,396
that we saw in the--
on that property slide,

1124
01:02:03,956 --> 01:02:06,536
we are picking a sign from the
first group, nonatomic come

1125
01:02:06,536 --> 01:02:08,446
in second group, and
readwrite from the third group.

1126
01:02:09,296 --> 01:02:12,076
And then we're saying that this
instance variable is called

1127
01:02:12,126 --> 01:02:12,726
int age.

1128
01:02:13,606 --> 01:02:17,636
So, assign again, you
can ignore nonatomic.

1129
01:02:17,636 --> 01:02:21,636
This is just saying
that like you can assume

1130
01:02:21,636 --> 01:02:24,046
that we're not dealing with
any multi-threaded type

1131
01:02:24,726 --> 01:02:26,136
messiness here.

1132
01:02:26,376 --> 01:02:28,846
You don't have to deal with
the overhead of making sure

1133
01:02:28,846 --> 01:02:32,276
that whenever we modify
this age instance field

1134
01:02:32,756 --> 01:02:35,636
that you don't have to deal
with making sure it's atomic.

1135
01:02:35,926 --> 01:02:40,286
So atomic just means like
it happens instantaneously.

1136
01:02:40,396 --> 01:02:41,366
We don't have to deal with that.

1137
01:02:42,276 --> 01:02:45,436
So, and readwrite,
this is probably

1138
01:02:45,906 --> 01:02:48,176
or in this class you're
pretty much only going

1139
01:02:48,176 --> 01:02:51,676
to using nonatomic.

1140
01:02:51,726 --> 01:02:58,036
Readwrite is one that if it's
readwrite then this property is

1141
01:02:58,036 --> 01:02:59,986
expecting a setter and a getter.

1142
01:03:00,296 --> 01:03:02,666
If it's read-only, it's
expecting just a getter.

1143
01:03:03,486 --> 01:03:05,996
So since we want to be
able to both write to age

1144
01:03:05,996 --> 01:03:08,176
and get the age, then
we say its readwrite

1145
01:03:08,366 --> 01:03:09,426
and some it will lead for name.

1146
01:03:09,996 --> 01:03:13,816
OK, so then these are the same.

1147
01:03:15,266 --> 01:03:19,206
Back in student.m
we see the same.

1148
01:03:19,706 --> 01:03:24,406
But looking at main.m, the fact

1149
01:03:24,606 --> 01:03:27,256
that these are now
properties allows us

1150
01:03:27,256 --> 01:03:31,076
to say Alice.age equals 20
and Alice.name equals @Alice.

1151
01:03:32,476 --> 01:03:37,046
So what do property
does for us is says

1152
01:03:37,346 --> 01:03:42,806
that since we said there is
a property called age then

1153
01:03:42,806 --> 01:03:47,736
Alice.age is by the conventions
that are now going to be

1154
01:03:47,736 --> 01:03:49,546
like something you
should absolutely follow,

1155
01:03:49,906 --> 01:03:52,376
Alice.age is underneath
the hood going

1156
01:03:52,376 --> 01:03:56,876
to be translated
to Alice setAge 20.

1157
01:03:58,546 --> 01:04:03,496
And this is going to
be Alice.name @Alice.

1158
01:04:04,986 --> 01:04:07,026
So this is what properties
have done for us.

1159
01:04:07,106 --> 01:04:08,796
It's allowed us to access

1160
01:04:08,796 --> 01:04:11,706
and set instance
variables using .notation.

1161
01:04:12,086 --> 01:04:15,186
Instead of having to go
through this set name

1162
01:04:15,186 --> 01:04:20,626
and setAge method
or message passing.

1163
01:04:20,626 --> 01:04:24,546
It's somewhat important and
yet at the same time not really

1164
01:04:24,546 --> 01:04:29,696
that important to
remember that we are going

1165
01:04:29,736 --> 01:04:31,676
through the overhead
of a message pass.

1166
01:04:31,746 --> 01:04:34,936
So it's not like in regular
C, this is not a struct,

1167
01:04:35,536 --> 01:04:38,846
so you would not be
able to use .notation

1168
01:04:38,846 --> 01:04:40,086
on a pointer to a struct.

1169
01:04:40,086 --> 01:04:45,826
This is an object, this
.notation is calling a method

1170
01:04:46,436 --> 01:04:50,926
and so there's some overhead to
that but again its Objective-C,

1171
01:04:51,366 --> 01:04:53,196
let the compiler handler--

1172
01:04:53,196 --> 01:04:55,656
let the compiler handle
any optimizations.

1173
01:04:55,656 --> 01:04:58,416
Don't really worry about the
overhead of message passings.

1174
01:04:58,946 --> 01:05:06,206
Then down here at greet,
we see s.name and s.age

1175
01:05:06,446 --> 01:05:11,126
which are equivalent to
just S name and S age.

1176
01:05:11,306 --> 01:05:13,506
And so this is why
I said before,

1177
01:05:13,566 --> 01:05:15,196
you should follow the convention

1178
01:05:15,196 --> 01:05:18,206
that like the getter is just
called name and not getname,

1179
01:05:18,716 --> 01:05:20,406
because s.name is going

1180
01:05:20,406 --> 01:05:23,206
to expect the getter
to be called name.

1181
01:05:23,586 --> 01:05:25,786
And if it's not, you're
not going to be able

1182
01:05:27,136 --> 01:05:28,866
to use the property as expected.

1183
01:05:29,516 --> 01:05:37,796
[ Inaudible Remark ]

1184
01:05:38,296 --> 01:05:41,086
Wait till the next example.

1185
01:05:41,666 --> 01:05:41,956
[laughs] Yeah.

1186
01:05:42,516 --> 01:05:50,056
[ Inaudible Remark ]

1187
01:05:50,556 --> 01:05:51,526
I changed that.

1188
01:05:51,776 --> 01:05:53,456
They should always have it.

1189
01:05:53,456 --> 01:05:57,226
Yeah. [Inaudible Remark]
So the reason for this is

1190
01:05:57,226 --> 01:06:01,316
that NSObject's init method, so
init is inherited from NSObject.

1191
01:06:01,766 --> 01:06:04,996
And we see that Student
is directly inheriting

1192
01:06:04,996 --> 01:06:09,276
from NSObject and so
because the init method

1193
01:06:09,276 --> 01:06:16,076
of NSObject does nothing,
then there is no difference

1194
01:06:16,076 --> 01:06:19,176
between Student alloc
and Student alloc init,

1195
01:06:19,176 --> 01:06:20,636
those two things are
going to be the same.

1196
01:06:21,556 --> 01:06:25,246
But you should always handle
initialized object if--

1197
01:06:25,946 --> 01:06:28,476
I doubt NSObject would do this,

1198
01:06:28,476 --> 01:06:30,966
but if someday NSObject
changes initialize method

1199
01:06:30,966 --> 01:06:34,716
to now keep track of something,
then this code will break

1200
01:06:34,716 --> 01:06:36,356
because you did not
initialize that object.

1201
01:06:36,806 --> 01:06:39,486
Or alternatively, if
inside of Student,

1202
01:06:39,876 --> 01:06:44,426
I decide to overrride the init
method and have it do something,

1203
01:06:46,076 --> 01:06:48,236
define my own init method,

1204
01:06:48,376 --> 01:06:52,276
we'll actually see
examples of that later.

1205
01:06:53,416 --> 01:06:55,886
But if I were to define
my own init method,

1206
01:06:56,146 --> 01:06:59,996
then all of a sudden, everything
in main.m would stop working

1207
01:06:59,996 --> 01:07:01,776
because it did not
initialize the Students.

1208
01:07:02,666 --> 01:07:05,766
So always-- even if you
know you're inheriting

1209
01:07:05,766 --> 01:07:07,926
from NSObject, still
initialize it.

1210
01:07:08,196 --> 01:07:10,306
Even if it's because
someone later on coming

1211
01:07:10,306 --> 01:07:12,486
around reading your code
is going to be like, "Wait,

1212
01:07:12,486 --> 01:07:15,216
this isn't initialized,"
now, they have to go check

1213
01:07:15,216 --> 01:07:16,126
to make sure that it--

1214
01:07:16,166 --> 01:07:19,116
the initialization isn't
actually going to do anything,

1215
01:07:19,116 --> 01:07:21,496
it's always initialized.

1216
01:07:22,516 --> 01:07:25,696
[ Pause ]

1217
01:07:26,196 --> 01:07:28,676
So one last.

1218
01:07:29,916 --> 01:07:31,566
OK. Any other questions?

1219
01:07:32,516 --> 01:07:35,446
[ Pause ]

1220
01:07:35,946 --> 01:07:39,836
OK. Then here.

1221
01:07:43,636 --> 01:07:52,786
OK. So synthesize, this is
what in the past was used

1222
01:07:52,826 --> 01:07:55,356
to do exactly what you
just said, that getting rid

1223
01:07:55,356 --> 01:07:56,436
of the getters and setters.

1224
01:07:56,846 --> 01:08:02,946
So, what-- let's
look at Student 4.

1225
01:08:03,516 --> 01:08:15,936
[ Pause ]

1226
01:08:16,436 --> 01:08:23,516
So we have gotten rid of
the explicit declarations

1227
01:08:23,516 --> 01:08:27,946
of the setters and getters
from here, just deleted them.

1228
01:08:28,906 --> 01:08:30,216
They were redundant
to begin with.

1229
01:08:30,756 --> 01:08:35,216
And Student-- or student.m,
we've now gotten rid

1230
01:08:35,216 --> 01:08:37,096
of the implementations
of those things.

1231
01:08:38,026 --> 01:08:40,096
In the past, we used init do

1232
01:08:40,096 --> 01:08:46,036
like @synthesize age equals
underscore age messiness.

1233
01:08:46,646 --> 01:08:49,056
The reason for that is
because the compiler didn't use

1234
01:08:49,056 --> 01:08:51,236
to be smart enough to recognize
that if there's a property

1235
01:08:51,236 --> 01:08:53,916
in here, then it should
automatically be in here.

1236
01:08:54,586 --> 01:08:56,096
Now, it is smart enough.

1237
01:08:56,096 --> 01:08:59,886
So now, the property
is enough to say

1238
01:08:59,886 --> 01:09:02,026
that we want an instance
variable called,

1239
01:09:02,656 --> 01:09:05,346
we're actually using
the defaults,

1240
01:09:05,776 --> 01:09:08,466
and this is again why we come
back to underscore, the defaults

1241
01:09:08,586 --> 01:09:12,706
of this line is, we want
an instance variable called

1242
01:09:12,936 --> 01:09:17,336
underscore age that has
a setter called setAge

1243
01:09:17,516 --> 01:09:19,476
and a setter-- a
getter called age.

1244
01:09:20,016 --> 01:09:23,636
And that setter and
getter is going

1245
01:09:23,636 --> 01:09:27,876
to be implemented using
these particular semantics

1246
01:09:28,196 --> 01:09:33,856
but you don't have to worry
about assign and nonatomic yet.

1247
01:09:34,236 --> 01:09:40,446
This also means that before,
even though we had set assign,

1248
01:09:40,446 --> 01:09:41,696
and nonatomic, and readwrite,

1249
01:09:42,636 --> 01:09:44,556
you are allowed to
override those.

1250
01:09:45,606 --> 01:09:49,506
And so before in
the previous example

1251
01:09:49,506 --> 01:09:51,696
when we had explicitly
defined the setAge,

1252
01:09:52,136 --> 01:09:55,636
setAge was not necessarily
following these semantics.

1253
01:09:56,076 --> 01:09:58,936
It happened to be because
that's how we implement it.

1254
01:09:58,936 --> 01:10:02,256
We didn't implement it carrying

1255
01:10:02,256 --> 01:10:03,566
about whether it
was multithreaded

1256
01:10:03,566 --> 01:10:04,406
and all that stuff.

1257
01:10:05,086 --> 01:10:09,486
So you are allowed to
override these behaviors.

1258
01:10:09,896 --> 01:10:13,746
I actually think Student
5 might go into that.

1259
01:10:14,516 --> 01:10:18,746
[ Pause ]

1260
01:10:19,246 --> 01:10:23,866
So Student 5, looking
at student.h, again,

1261
01:10:23,866 --> 01:10:25,226
we still have age and name.

1262
01:10:26,206 --> 01:10:30,116
Now, in student.m, we
still do not synthesize

1263
01:10:30,836 --> 01:10:35,596
but we just happen to decide to
overwrite the setName method.

1264
01:10:35,596 --> 01:10:41,936
This was David's example of if
the name happens to be David,

1265
01:10:41,986 --> 01:10:44,186
then change it to
Dummy, otherwise,

1266
01:10:44,486 --> 01:10:45,826
do the original behavior.

1267
01:10:46,776 --> 01:10:52,026
So here, we have overwritten
what the default setting would

1268
01:10:52,026 --> 01:10:56,466
do if we just did
copy nonatomic.

1269
01:10:57,576 --> 01:11:02,446
But, if we also decide
that we wanted to--

1270
01:11:03,516 --> 01:11:14,016
[ Pause ]

1271
01:11:14,516 --> 01:11:19,746
If we decided that we wanted
to override the getter,

1272
01:11:20,556 --> 01:11:26,426
and say return name, now,
because of the way things are,

1273
01:11:26,776 --> 01:11:29,396
we do need to explicitly
synthesize.

1274
01:11:30,106 --> 01:11:31,476
So, now, we would need

1275
01:11:31,476 --> 01:11:36,146
to say synthesize name
equals underscore name.

1276
01:11:36,596 --> 01:11:38,866
And those should go away.

1277
01:11:40,036 --> 01:11:40,416
All right.

1278
01:11:40,486 --> 01:11:43,106
It's only if you happen to
be overriding both the getter

1279
01:11:43,106 --> 01:11:46,246
and the setter that we need to
synthesize the variable again.

1280
01:11:46,976 --> 01:11:48,516
Otherwise, it wouldn't know

1281
01:11:48,516 --> 01:11:50,666
that this underscore
name variable exists.

1282
01:11:51,856 --> 01:11:55,796
This behavior is because of some
backwards compatibility issues

1283
01:11:55,796 --> 01:11:57,836
but it's the way it is.

1284
01:11:58,516 --> 01:12:04,116
[ Inaudible Remark ]

1285
01:12:04,616 --> 01:12:07,686
But if you use property
and you say readwrite,

1286
01:12:07,686 --> 01:12:10,516
if it is a readwrite
property, the getter is--

1287
01:12:10,576 --> 01:12:13,466
getters and setters are
automatically defined before you

1288
01:12:13,726 --> 01:12:15,446
unless you decide
to override them.

1289
01:12:15,976 --> 01:12:18,816
And if you override
them both, then you need

1290
01:12:18,816 --> 01:12:22,226
to synthesize again, because
otherwise it doesn't know

1291
01:12:22,226 --> 01:12:23,506
about that instance variable.

1292
01:12:24,156 --> 01:12:30,256
It's like looking at
student.h, we see property age

1293
01:12:30,256 --> 01:12:32,936
and property name, we don't see
anything about underscore age

1294
01:12:32,936 --> 01:12:35,956
and underscore name,
and that's part

1295
01:12:35,956 --> 01:12:38,126
of the reason why we
need to synthesize.

1296
01:12:38,126 --> 01:12:41,066
It's messy but rarely
are you going

1297
01:12:41,066 --> 01:12:43,296
to be overriding both the
getter and the setter anyway.

1298
01:12:43,986 --> 01:12:45,296
You might be overriding
them in--

1299
01:12:45,296 --> 01:12:48,776
if you are like trying to debug
something and you add NSLog

1300
01:12:48,776 --> 01:12:51,286
to both the getter and
the setter, in which case,

1301
01:12:51,846 --> 01:12:55,286
remember that now, you're going
to need to synthesize or self,

1302
01:12:55,286 --> 01:12:56,286
it'll [inaudible] you.

1303
01:12:58,236 --> 01:12:58,936
Questions?

1304
01:12:59,516 --> 01:13:05,416
[ Inaudible Remark ]

1305
01:13:05,916 --> 01:13:06,986
Yeah. See?

1306
01:13:06,986 --> 01:13:08,686
It-- well, if I get
rid of the assign, now,

1307
01:13:08,686 --> 01:13:10,026
I'm going to get
a bunch of errors.

1308
01:13:10,666 --> 01:13:15,926
If I now get rid of this setter,
I don't have any errors anymore,

1309
01:13:17,136 --> 01:13:20,656
because as long as I only
override one of them, it's fine.

1310
01:13:21,356 --> 01:13:25,326
It's also important just
to recognize synthesize

1311
01:13:25,326 --> 01:13:28,076
because you're going to see
it in a lot of prior examples,

1312
01:13:28,136 --> 01:13:32,386
like a lot of online things
you find will use synthesize,

1313
01:13:32,386 --> 01:13:35,656
'cause it's a relatively
new thing.

1314
01:13:36,656 --> 01:13:38,866
But you don't need to use it.

1315
01:13:40,926 --> 01:13:41,456
OK.

1316
01:13:42,516 --> 01:13:48,916
[ Pause ]

1317
01:13:49,416 --> 01:13:51,856
OK. So, init methods.

1318
01:13:52,406 --> 01:13:55,256
So up till now, we
just used init.

1319
01:13:55,926 --> 01:13:57,606
Remember, you should
always initialize something

1320
01:13:57,826 --> 01:14:00,126
but you don't need to
initialize it with just init.

1321
01:14:00,126 --> 01:14:03,186
You can define your own
initialization methods.

1322
01:14:03,186 --> 01:14:07,646
And if you look at-- well,
when you're actually doing iOS,

1323
01:14:07,646 --> 01:14:08,926
you're going to be
dealing with a bunch

1324
01:14:08,926 --> 01:14:10,346
of other initialization methods.

1325
01:14:10,916 --> 01:14:14,696
But the convention is that
the initialization methods are

1326
01:14:14,696 --> 01:14:19,256
always going to start with
lower case init, and then, the--

1327
01:14:19,256 --> 01:14:24,356
so, reading this method, we have
an instance method 'cause it's

1328
01:14:24,356 --> 01:14:24,876
a dash.

1329
01:14:25,766 --> 01:14:31,346
It's going to return an ID,
so this is also convention.

1330
01:14:31,636 --> 01:14:35,126
We know that it is necessarily
returning a Student star,

1331
01:14:36,326 --> 01:14:38,006
and remember that
ID is just sort

1332
01:14:38,006 --> 01:14:39,856
of like a catch all
for all objects.

1333
01:14:40,376 --> 01:14:42,296
The convention

1334
01:14:42,296 --> 01:14:44,326
for initialization methods
is that they return ID.

1335
01:14:44,326 --> 01:14:48,986
Then the-- this is pretty
much the convention too

1336
01:14:49,206 --> 01:14:54,886
that we have initWithName:
and we pass it a name.

1337
01:14:55,236 --> 01:14:57,946
And notice now, we are
passing a second argument,

1338
01:14:58,236 --> 01:14:59,856
so space andAge:age.

1339
01:14:59,856 --> 01:15:05,976
So the name of this method
is initWithName:andAge:.

1340
01:15:05,976 --> 01:15:08,936
That name and age are arguments.

1341
01:15:08,986 --> 01:15:10,486
Those are not part of
the name of method.

1342
01:15:11,466 --> 01:15:13,796
So, we already see how these
names can get pretty long.

1343
01:15:14,536 --> 01:15:16,586
Believe me when I say that
there are method names that are

1344
01:15:16,586 --> 01:15:22,476
like four times longer
than this in iOS dealings

1345
01:15:22,576 --> 01:15:24,576
that you will be using.

1346
01:15:26,556 --> 01:15:32,056
So, one other-- I don't know.

1347
01:15:33,676 --> 01:15:34,076
Questions.

1348
01:15:34,076 --> 01:15:34,416
[Inaudible Remark] Yes.

1349
01:15:34,416 --> 01:15:37,476
It's very similar to void
star except it's dealing

1350
01:15:37,476 --> 01:15:39,686
with objects instead
of general pointers.

1351
01:15:40,216 --> 01:15:43,126
And we'll see ID in
other places too.

1352
01:15:43,976 --> 01:15:45,326
And it's also similar
to voidstar

1353
01:15:45,326 --> 01:15:48,886
and that automatic
casting happens.

1354
01:15:51,016 --> 01:15:51,896
[Inaudible Remark]
This whole thing?

1355
01:15:52,116 --> 01:15:57,356
OK. So, we have instance method.

1356
01:15:57,356 --> 01:16:00,946
It is returning an ID so
that's just the way it is.

1357
01:16:00,946 --> 01:16:03,226
If you wanted, you
could say Student star,

1358
01:16:03,606 --> 01:16:06,156
but you shouldn't--
you should say ID

1359
01:16:06,156 --> 01:16:08,986
because any initialization
method pretty much returns ID,

1360
01:16:10,456 --> 01:16:14,736
then the convention is that
you're going to init and then

1361
01:16:15,676 --> 01:16:17,406
with is also pretty
much the convention.

1362
01:16:17,406 --> 01:16:21,506
The-- any colon-- you
can count the colons

1363
01:16:21,506 --> 01:16:23,286
to determine the number of
arguments to your function.

1364
01:16:23,806 --> 01:16:25,496
So here we see two colons.

1365
01:16:25,696 --> 01:16:27,686
That means we know they're
going to be two arguments.

1366
01:16:28,956 --> 01:16:30,946
The first argument
is called name,

1367
01:16:30,946 --> 01:16:32,356
and a subtype NSString star.

1368
01:16:32,826 --> 01:16:35,186
The second argument is called
age, and a subtype int.

1369
01:16:36,156 --> 01:16:39,646
So, when we want to
actually call this function,

1370
01:16:39,816 --> 01:16:43,456
we might say like, well,
first, we allocate a Student

1371
01:16:44,036 --> 01:16:53,466
and then we say, Student
initWithName:@bob andAge:21,

1372
01:16:54,016 --> 01:16:59,126
and we'll see an example of that

1373
01:16:59,366 --> 01:17:02,616
in another Students
4 or 5 or whatever.

1374
01:17:03,516 --> 01:17:16,736
[ Inaudible Remark ]

1375
01:17:17,236 --> 01:17:22,396
So, the answer is no, but
that should never be the case

1376
01:17:22,996 --> 01:17:24,566
because if the variables
are different,

1377
01:17:24,656 --> 01:17:26,086
this naming scheme
should be different.

1378
01:17:26,546 --> 01:17:29,726
This is how you name
methods in like--

1379
01:17:29,836 --> 01:17:33,366
all method names are very long
and explicit in Objective-C.

1380
01:17:33,636 --> 01:17:36,246
And so, if we decided
that instead,

1381
01:17:36,246 --> 01:17:38,466
we wanted to just
initialize with the name,

1382
01:17:38,676 --> 01:17:42,206
then we would have initWithName
method without the andAge.

1383
01:17:42,466 --> 01:17:45,586
If we wanted to just do name,
we would've initWithAge method

1384
01:17:45,946 --> 01:17:50,916
without the andName or whatever.

1385
01:17:50,916 --> 01:17:50,983
Yeah.

1386
01:17:51,516 --> 01:17:54,916
[ Inaudible Remark ]

1387
01:17:55,416 --> 01:17:57,006
That was a typo.

1388
01:17:57,406 --> 01:17:58,196
That has been fixed.

1389
01:17:59,126 --> 01:18:00,746
These will be uploaded
at the end.

1390
01:18:01,496 --> 01:18:02,826
There are some changes.

1391
01:18:03,426 --> 01:18:06,146
Other questions.

1392
01:18:07,166 --> 01:18:07,316
Yeah.

1393
01:18:08,516 --> 01:18:13,866
[ Inaudible Remark ]

1394
01:18:14,366 --> 01:18:15,996
Yeah. You would just put more

1395
01:18:16,086 --> 01:18:19,786
with andGender, andYear
and whatever.

1396
01:18:20,526 --> 01:18:25,136
The number of colons specifies--
in theory, we wouldn't--

1397
01:18:25,316 --> 01:18:30,176
we don't need to say andAge like
a valid method name is init,

1398
01:18:30,856 --> 01:18:34,156
colon, colon which--
because there are two colons,

1399
01:18:34,156 --> 01:18:37,636
we expect there are going to be
two arguments, and so we call it

1400
01:18:37,636 --> 01:18:41,966
with like init:bob:20, but
that's not what you should do

1401
01:18:41,966 --> 01:18:44,586
in Objective-C, you
should explicitly name all

1402
01:18:44,586 --> 01:18:45,436
of your parameters.

1403
01:18:45,686 --> 01:18:50,526
So, initWithName:whatever,
andAge:whatever.

1404
01:18:52,516 --> 01:18:58,476
[ Inaudible Remark ]

1405
01:18:58,976 --> 01:19:03,886
You cannot by default pass in a
dictionary or object or anything

1406
01:19:03,886 --> 01:19:06,276
like that, this is pretty much
the only way you can run it,

1407
01:19:07,126 --> 01:19:10,176
unless you happened to
define initialization method

1408
01:19:10,176 --> 01:19:14,016
that like takes what we'll see
later like in NSDictionary,

1409
01:19:14,526 --> 01:19:17,176
but that's up to you to define
such an initialization method.

1410
01:19:17,636 --> 01:19:21,006
It's-- but you pretty
much should--

1411
01:19:21,006 --> 01:19:22,026
this is the way it should be.

1412
01:19:22,416 --> 01:19:23,966
And also, if you
follow this convention,

1413
01:19:23,966 --> 01:19:26,996
I'm pretty sure Xcode is
going to give you some like--

1414
01:19:27,426 --> 01:19:30,616
some more advanced tips in
Auto Completion and stuff.

1415
01:19:31,936 --> 01:19:34,426
The convention being like
they should return ID,

1416
01:19:34,836 --> 01:19:36,606
they should start with
the four letters, init.

1417
01:19:37,416 --> 01:19:41,906
And when someone is reading
your code, they expect you

1418
01:19:41,906 --> 01:19:44,256
to initialize something so they
should see the four letters,

1419
01:19:44,256 --> 01:19:44,656
init.

1420
01:19:49,506 --> 01:19:52,326
OK. Looking at this example.

1421
01:19:53,516 --> 01:19:58,576
[ Pause ]

1422
01:19:59,076 --> 01:20:01,546
Six. OK.

1423
01:20:02,516 --> 01:20:11,236
[ Pause ]

1424
01:20:11,736 --> 01:20:16,166
OK. So, in student.h, we see we
are now declaring a new method,

1425
01:20:16,836 --> 01:20:18,156
exactly what we saw before.

1426
01:20:18,626 --> 01:20:22,826
Now that we have said that
we are using this method,

1427
01:20:23,306 --> 01:20:27,676
we need to implement that
method in the .m file.

1428
01:20:27,866 --> 01:20:31,916
So, here, let's first
look at the initWithName.

1429
01:20:32,466 --> 01:20:34,846
So, this is a very
common pattern.

1430
01:20:35,946 --> 01:20:37,926
Whenever you define
your own initializer,

1431
01:20:38,016 --> 01:20:42,706
this is pretty much the pattern
you want to take where the--

1432
01:20:43,516 --> 01:20:50,186
[ Pause ]

1433
01:20:50,686 --> 01:20:52,676
-- first, second--
let's say this first.

1434
01:20:52,676 --> 01:20:56,176
So, this is somewhat
weird from other languages

1435
01:20:56,176 --> 01:20:58,696
where like self is just
sort of like a keyword.

1436
01:20:58,946 --> 01:21:01,106
Here, self is a variable
and you're assigning

1437
01:21:01,106 --> 01:21:04,916
to that variable the result of
initializing your super class.

1438
01:21:05,506 --> 01:21:08,076
So, this is the part
of the pattern

1439
01:21:08,076 --> 01:21:09,436
that you pretty much always do.

1440
01:21:09,916 --> 01:21:11,966
So, just because like--

1441
01:21:12,796 --> 01:21:15,336
or I happened to
inherit from NSObject

1442
01:21:15,336 --> 01:21:17,566
which its initialization
method does nothing,

1443
01:21:17,796 --> 01:21:19,316
but imagine that it
does do something.

1444
01:21:19,936 --> 01:21:23,866
So, just because
I know how I want

1445
01:21:23,866 --> 01:21:27,106
to be initialized doesn't mean
I know how my parent class wants

1446
01:21:27,106 --> 01:21:27,836
to be initialized.

1447
01:21:27,836 --> 01:21:30,346
So, you should always
call your parent--

1448
01:21:30,346 --> 01:21:34,206
initializer of parent class.

1449
01:21:34,316 --> 01:21:37,026
So, the reason we assigned
to self and do this whole

1450
01:21:37,026 --> 01:21:41,166
if self equals super init thing
is under some bizarre scenario

1451
01:21:41,166 --> 01:21:44,486
where initializing the super
class manages to return nil

1452
01:21:44,486 --> 01:21:47,976
or something, like
initialization went wrong,

1453
01:21:48,506 --> 01:21:50,496
then we don't want to
do these two things.

1454
01:21:51,436 --> 01:21:53,676
As an example, maybe
we just happened

1455
01:21:53,676 --> 01:21:57,896
to define the initializer
as like only five objects

1456
01:21:57,896 --> 01:21:59,926
of this class type should
ever be initialized.

1457
01:22:00,126 --> 01:22:01,636
After five, it should just fail.

1458
01:22:02,436 --> 01:22:07,606
And so-- then when they tried to
initialize, if it's after five,

1459
01:22:07,706 --> 01:22:10,326
presumably this super
init is going to fail

1460
01:22:10,606 --> 01:22:16,986
and self will be nil and
then will just return self.

1461
01:22:16,986 --> 01:22:17,656
Yes? [Inaudible Remark] Yes.

1462
01:22:17,656 --> 01:22:22,516
Yes, yes. Self, this are two
things you see in languages,

1463
01:22:22,516 --> 01:22:26,126
there is no this in Objective-C,
they just call it self.

1464
01:22:26,246 --> 01:22:30,036
It means like-- it means
this current object.

1465
01:22:30,216 --> 01:22:36,686
So, when I say-- when
I have a Student,

1466
01:22:36,686 --> 01:22:39,066
like I just allocated the
Student and then I passed

1467
01:22:39,356 --> 01:22:42,006
that Student the message
initWithName, whatever,

1468
01:22:42,506 --> 01:22:46,466
then self refers to that Student
I just passed the message to.

1469
01:22:47,786 --> 01:22:50,026
And so, similarly, self.age

1470
01:22:50,566 --> 01:22:57,156
which is also equivalent
to self setAge, age.

1471
01:22:58,246 --> 01:23:02,516
So, self.age refers
to this student.

1472
01:23:03,486 --> 01:23:07,216
We want to set our own age
instance variable to age.

1473
01:23:09,516 --> 01:23:18,686
[ Inaudible Remark ]

1474
01:23:19,186 --> 01:23:21,296
Which do you need spell out?

1475
01:23:21,526 --> 01:23:21,806
Spell out.

1476
01:23:22,776 --> 01:23:23,626
[Inaudible Remark] This?

1477
01:23:24,206 --> 01:23:31,336
So, this gets into that--
there should always be--

1478
01:23:31,336 --> 01:23:35,096
and you can look up these exact
words designated initializer.

1479
01:23:35,776 --> 01:23:39,686
So, not really going to go
into deep detail here, but,

1480
01:23:40,506 --> 01:23:43,586
every class should have exactly
one designated initializer.

1481
01:23:44,346 --> 01:23:46,766
And this is, in other
languages, you might see it

1482
01:23:46,766 --> 01:23:49,326
as like you define
one constructor,

1483
01:23:49,466 --> 01:23:50,986
that sort of all
other constructors

1484
01:23:50,986 --> 01:23:52,066
that you have called.

1485
01:23:53,296 --> 01:23:55,306
And so here, we decide

1486
01:23:55,306 --> 01:23:59,066
that initWithName should be
our designated initializer,

1487
01:23:59,506 --> 01:24:03,186
which means that init needs to
call our designated initializer.

1488
01:24:03,616 --> 01:24:14,246
If we then defined a
(id)initWithAge method,

1489
01:24:16,456 --> 01:24:25,736
that should be int, then this
must also call our designated

1490
01:24:25,736 --> 01:24:30,156
initializer and we'll
use the defaults above,

1491
01:24:30,936 --> 01:24:33,636
beyond andAge, age.

1492
01:24:40,636 --> 01:24:42,466
So, there should always
be one initializer

1493
01:24:42,466 --> 01:24:44,166
which is your designated
initializer.

1494
01:24:44,866 --> 01:24:48,626
And also, that designated
initializer needs

1495
01:24:48,626 --> 01:24:50,376
to call the designated
initializer

1496
01:24:50,376 --> 01:24:51,646
of your parent class.

1497
01:24:52,466 --> 01:24:53,646
So, notice that init

1498
01:24:53,646 --> 01:24:55,766
and initWithAge are
not calling super init,

1499
01:24:56,096 --> 01:24:57,666
it's only our designated
initializer

1500
01:24:57,666 --> 01:24:58,806
which is calling super init.

1501
01:24:59,866 --> 01:25:04,556
So that's just-- if you start
to find your own initializer,

1502
01:25:04,556 --> 01:25:06,376
just make sure you
follow this convention

1503
01:25:06,376 --> 01:25:09,106
and you can remember just
googling designated initializer.

1504
01:25:09,556 --> 01:25:16,466
And also notice that
I should not do this.

1505
01:25:17,036 --> 01:25:18,856
Why is it?

1506
01:25:19,136 --> 01:25:23,416
I should not do this because
this is not appropriately

1507
01:25:23,416 --> 01:25:24,096
setting self.

1508
01:25:25,026 --> 01:25:29,386
So, we want to set self
to whatever that happens

1509
01:25:29,386 --> 01:25:31,586
to return and then return self.

1510
01:25:32,366 --> 01:25:34,366
[ Pause ]

1511
01:25:34,716 --> 01:25:35,426
Questions?

1512
01:25:36,906 --> 01:25:39,356
And then going back to main.

1513
01:25:40,036 --> 01:25:44,536
Now, we see that all in one
line we're allocating a student,

1514
01:25:44,606 --> 01:25:46,926
initializing it with a
name Alice and the age 20.

1515
01:25:53,496 --> 01:25:55,086
>> So the things we're
allowing is super--

1516
01:25:55,086 --> 01:25:55,796
>> In here?

1517
01:25:55,826 --> 01:25:55,893
>> Yeah.

1518
01:25:56,016 --> 01:26:02,526
>> So, imagine we are-- go back
to the example from a while ago

1519
01:26:02,526 --> 01:26:04,876
where were have like
NSObject and we have person,

1520
01:26:04,876 --> 01:26:06,466
then we have student,
then we've 8th grader.

1521
01:26:07,246 --> 01:26:10,036
So, I want to define a
new initialization method

1522
01:26:10,036 --> 01:26:15,616
for 8th grader and I decide
that to initialize it like say,

1523
01:26:15,616 --> 01:26:17,476
it has an instant 8th graders.

1524
01:26:17,546 --> 01:26:18,576
Let say kindergardener.

1525
01:26:18,936 --> 01:26:21,826
So, kindergardener has an
instance variable favorite color

1526
01:26:22,216 --> 01:26:26,556
and so we decided that like
the default initialize--

1527
01:26:26,636 --> 01:26:28,466
the default value for that
instance variable should

1528
01:26:28,466 --> 01:26:28,946
be blue.

1529
01:26:29,556 --> 01:26:32,526
So, we define an
initializer that says,

1530
01:26:32,926 --> 01:26:38,176
"That should be blue" except if
we don't then call super init,

1531
01:26:38,476 --> 01:26:42,016
super now refers to, if
we are a kindergarderner,

1532
01:26:42,016 --> 01:26:43,556
super refers to student.

1533
01:26:44,106 --> 01:26:47,406
And so if we don't call super
init then we're not initializing

1534
01:26:47,406 --> 01:26:49,936
all these instance variables
that happened to come

1535
01:26:50,256 --> 01:26:52,986
from the student class,
in the person class,

1536
01:26:52,986 --> 01:26:54,506
in the NSObjects class.

1537
01:26:55,166 --> 01:27:00,596
So, you should always
initialize your superclass.

1538
01:27:01,256 --> 01:27:03,436
Similar to like you should
always initialize any object

1539
01:27:03,436 --> 01:27:05,686
that you allocate, you
should always initialize your

1540
01:27:05,686 --> 01:27:10,376
superclass and these
two methods happen

1541
01:27:10,376 --> 01:27:11,786
to initialize the superclass

1542
01:27:12,166 --> 01:27:14,396
by calling the designated
initializer

1543
01:27:14,656 --> 01:27:16,976
which initializes
the superclass.

1544
01:27:17,586 --> 01:27:20,826
But again, this pattern
like this is the pattern

1545
01:27:20,826 --> 01:27:23,556
for writing your
own initializer.

1546
01:27:23,866 --> 01:27:28,516
So, we should almost always
copy and paste this and then

1547
01:27:28,516 --> 01:27:30,166
like in these curly braces is

1548
01:27:30,166 --> 01:27:32,176
where you set your
instance variables

1549
01:27:32,176 --> 01:27:34,406
or NSLog whenever it
is you want an NSLog.

1550
01:27:35,396 --> 01:27:37,396
[ Inaudible Remark ]

1551
01:27:37,776 --> 01:27:37,876
>> Yeah.

1552
01:27:38,516 --> 01:27:47,616
[ Pause ]

1553
01:27:48,116 --> 01:27:50,596
'Cause there might be something
in that superclass that is

1554
01:27:50,596 --> 01:27:52,946
like essential for
everything else to work.

1555
01:27:53,516 --> 01:28:03,886
[ Pause ]

1556
01:28:04,386 --> 01:28:04,976
Any other questions?

1557
01:28:05,516 --> 01:28:12,636
[ Inaudible Remark ]

1558
01:28:13,136 --> 01:28:15,646
>> Why does init
with age not needed?

1559
01:28:16,276 --> 01:28:18,596
Because it's calling
init with name and age.

1560
01:28:19,036 --> 01:28:22,476
It's calling this method which
itself call super init, OK.

1561
01:28:24,516 --> 01:28:28,896
[ Pause ]

1562
01:28:29,396 --> 01:28:31,056
OK. We'll take the
five minute break.

1563
01:28:33,016 --> 01:28:37,766
OK. So, am I-- am I now louder?

1564
01:28:39,706 --> 01:28:43,726
So, now, talk about
collections for just a second.

1565
01:28:44,006 --> 01:28:46,436
The only ones we're going to
see right now are these NSArray

1566
01:28:46,436 --> 01:28:52,896
and NSMutableArray where
NSArray is in MutableArray

1567
01:28:53,306 --> 01:28:55,826
which pretty much means
like one set exists.

1568
01:28:56,256 --> 01:28:56,976
It is what it is.

1569
01:28:56,976 --> 01:28:57,806
You can't change it.

1570
01:28:57,806 --> 01:29:00,446
MutableArray is going to be
useful and that we're going

1571
01:29:00,446 --> 01:29:03,006
to be able to just keep
adding things to it.

1572
01:29:03,646 --> 01:29:05,496
NSDictionary and
NSMutableDictionary,

1573
01:29:05,496 --> 01:29:07,706
so those are going to be useful

1574
01:29:07,766 --> 01:29:13,366
for like KeyValuePair type
stuff, associative arrays, maps,

1575
01:29:13,366 --> 01:29:15,076
whatever, hash tables,
whatever you want to call them,

1576
01:29:15,076 --> 01:29:16,416
that's what you're
going to use those for.

1577
01:29:16,946 --> 01:29:19,256
Set is just another
example of the collection.

1578
01:29:19,546 --> 01:29:22,316
NSArray is going to be
the most commonly used

1579
01:29:22,316 --> 01:29:24,136
and we're going to
use it right here.

1580
01:29:24,136 --> 01:29:25,966
So, looking at the next example.

1581
01:29:26,516 --> 01:29:39,026
[ Pause ]

1582
01:29:39,526 --> 01:29:42,976
OK. So, Student.h and
Student.m haven't changed.

1583
01:29:44,096 --> 01:29:47,536
It's main.m. Now, we're trying
to do something interesting

1584
01:29:48,446 --> 01:29:53,176
where we have a NSMutableArray
of students

1585
01:29:53,836 --> 01:29:56,386
and we're just allocate--
allocating it and initting it.

1586
01:29:56,896 --> 01:30:00,526
And then we want to insert
into that array Alice and Bob.

1587
01:30:00,996 --> 01:30:02,806
So we've done it in one line.

1588
01:30:02,806 --> 01:30:04,426
If we wanted, we can
do the same thing.

1589
01:30:04,426 --> 01:30:04,966
We can do--

1590
01:30:05,516 --> 01:30:25,546
[ Pause ]

1591
01:30:26,046 --> 01:30:33,196
So, the NSMutableArray class
defines an instance method

1592
01:30:33,326 --> 01:30:35,756
called AddObject
that is just going

1593
01:30:35,756 --> 01:30:39,576
to take some argument
with type ID.

1594
01:30:39,976 --> 01:30:42,396
And so since student
star is an object type,

1595
01:30:42,636 --> 01:30:44,036
that isn't appropriate ID.

1596
01:30:45,426 --> 01:30:47,986
And then we're going to
do that again with Bob.

1597
01:30:47,986 --> 01:30:50,906
So you name this as an array
where the first element is Alice

1598
01:30:50,906 --> 01:30:52,096
and the second element is Bob.

1599
01:30:53,166 --> 01:30:57,536
And then here is I
referenced this a while ago

1600
01:30:57,856 --> 01:31:02,326
that we have this for in
syntax that we're able

1601
01:31:02,326 --> 01:31:04,076
to use with collections.

1602
01:31:04,876 --> 01:31:07,176
So this well also wok for
dictionaries and sets.

1603
01:31:07,596 --> 01:31:12,736
So for each student in
this NSMutableArray we want

1604
01:31:12,736 --> 01:31:14,256
to greet that student.

1605
01:31:15,036 --> 01:31:19,716
So one thing that's somewhat
important to remember is

1606
01:31:19,716 --> 01:31:25,766
that everything inside of this
array is just stored as an ID,

1607
01:31:26,186 --> 01:31:28,956
like there is nothing enforcing

1608
01:31:28,956 --> 01:31:31,246
that I only put students
into this array.

1609
01:31:31,656 --> 01:31:36,226
If I wanted, I can put a
student and then NSString

1610
01:31:36,226 --> 01:31:37,276
and then another student

1611
01:31:37,276 --> 01:31:41,016
and then some
UITableViewController that's

1612
01:31:41,156 --> 01:31:45,686
it's up to you to enforce that
everything is of the same type

1613
01:31:46,296 --> 01:31:47,166
or if you don't want to enforce

1614
01:31:47,166 --> 01:31:48,896
that you then just make
sure it's makes sense.

1615
01:31:48,896 --> 01:31:54,066
You know what you're doing, but
like if I wanted I could say,

1616
01:31:55,496 --> 01:31:59,936
this and this isn't
going to work

1617
01:31:59,936 --> 01:32:05,696
because NSString doesn't have a
name and age instance variable,

1618
01:32:05,996 --> 01:32:09,966
but it doesn't know what's
inside of the students array.

1619
01:32:09,966 --> 01:32:12,746
So for all the compiler
knows, it is an NSString.

1620
01:32:14,436 --> 01:32:17,136
This is where it can be somewhat
dangerous dealing with ID

1621
01:32:17,676 --> 01:32:21,796
and this is also why you might
need to be dealing with casts.

1622
01:32:22,126 --> 01:32:24,926
So, you might want some
cast these return values

1623
01:32:24,926 --> 01:32:26,566
to the appropriate things.

1624
01:32:27,606 --> 01:32:31,536
Okay. So, questions on this
iterating over an array?

1625
01:32:32,746 --> 01:32:32,976
Yeah.

1626
01:32:33,516 --> 01:32:35,576
[ Inaudible Remark ]

1627
01:32:36,076 --> 01:32:40,316
So it's-- how it's working
underneath the hood is

1628
01:32:40,316 --> 01:32:44,216
that this NSMutableArray
class needs

1629
01:32:44,216 --> 01:32:47,006
to define how to
iterate over it.

1630
01:32:47,496 --> 01:32:51,236
And so in this method that it's
defining for how to iterate

1631
01:32:51,236 --> 01:32:53,296
over it, it's saying that like,

1632
01:32:53,506 --> 01:32:55,836
"I'm going to do
it in this order."

1633
01:32:55,836 --> 01:32:58,836
So it's saying that like
index 0 is the first thing

1634
01:32:58,836 --> 01:33:00,956
that should be return
then index 1 then index 2.

1635
01:33:00,956 --> 01:33:06,166
>> Is there any way for
that to [inaudible] like-

1636
01:33:06,296 --> 01:33:14,186
>> So, we'll see-- I don't think
there is to-- directly to erase.

1637
01:33:14,826 --> 01:33:18,926
We'll see though that protocols
at the very least are ways

1638
01:33:18,926 --> 01:33:20,686
to make IDs a little stronger.

1639
01:33:21,446 --> 01:33:25,066
There are ways to make sure that
these IDs are at least objects

1640
01:33:25,066 --> 01:33:26,966
that conform to a
specific interface.

1641
01:33:27,806 --> 01:33:33,996
I don't think there's any
generic type construct

1642
01:33:33,996 --> 01:33:34,636
in the language.

1643
01:33:35,516 --> 01:33:39,266
[ Pause ]

1644
01:33:39,766 --> 01:33:39,966
Okay.

1645
01:33:40,516 --> 01:33:47,076
[ Pause ]

1646
01:33:47,576 --> 01:33:49,566
Okay. So that's the
syntax we just saw

1647
01:33:49,826 --> 01:33:54,376
and notice here we are
saying for ID foo in bar

1648
01:33:54,746 --> 01:33:58,136
and so we don't know
what is inside a bar

1649
01:33:58,136 --> 01:34:00,946
and so all we can really
say is ID unless you happen

1650
01:34:00,946 --> 01:34:01,976
to know what it really is.

1651
01:34:02,516 --> 01:34:06,626
[ Pause ]

1652
01:34:07,126 --> 01:34:10,526
Okay. So, categories is a--

1653
01:34:10,836 --> 01:34:12,686
actually, let's ignore
that for a second

1654
01:34:12,686 --> 01:34:13,976
and let's do another
example first.

1655
01:34:14,516 --> 01:34:16,936
[ Pause ]

1656
01:34:17,436 --> 01:34:18,736
Student 8.

1657
01:34:19,516 --> 01:34:26,676
[ Pause ]

1658
01:34:27,176 --> 01:34:31,346
Okay. So the point in this
example is I've decided I don't

1659
01:34:31,346 --> 01:34:36,396
want to print these things in
just whatever order I happen

1660
01:34:36,396 --> 01:34:37,836
to insert them into the array.

1661
01:34:37,956 --> 01:34:40,096
I first want to sort the
array and then I want

1662
01:34:40,096 --> 01:34:41,226
to print out the sorted array.

1663
01:34:41,496 --> 01:34:45,686
So, there are couple
of ways we can do that.

1664
01:34:45,686 --> 01:34:50,376
I inserted them in the order,
RJ, Chris, Rob, David, and so,

1665
01:34:50,626 --> 01:34:52,116
here by looking

1666
01:34:52,116 --> 01:34:55,076
at the NSMutableArray
documentation I see

1667
01:34:55,076 --> 01:34:58,096
that it has a whole
bunch of different ways

1668
01:34:58,096 --> 01:34:59,306
of actually sorting things.

1669
01:35:00,416 --> 01:35:04,196
One way is this sorted
array using selector method.

1670
01:35:04,276 --> 01:35:09,436
First, let's look at
Student.h and Student.m. So,

1671
01:35:09,646 --> 01:35:10,916
inside of Student.h,

1672
01:35:10,976 --> 01:35:15,166
I have defined a new
method called compare.

1673
01:35:15,996 --> 01:35:18,546
It's returning this
NSComparisonResult,

1674
01:35:18,816 --> 01:35:21,526
and the only reason
that's relevant is

1675
01:35:21,526 --> 01:35:27,936
because this sorted array using
selector method expects a method

1676
01:35:27,936 --> 01:35:32,686
to be passed to it that
returns an NSComparator result,

1677
01:35:32,686 --> 01:35:34,696
whatever it is.

1678
01:35:34,926 --> 01:35:39,486
So, this compare method is
going to take another student

1679
01:35:39,966 --> 01:35:42,816
and just compare ourself,

1680
01:35:42,816 --> 01:35:44,666
our current student
with that student.

1681
01:35:44,836 --> 01:35:47,306
Looking at the implementation
of that,

1682
01:35:48,436 --> 01:35:51,106
I decided that I'm just
going to compare it by name.

1683
01:35:51,516 --> 01:35:57,216
So, I'm going to use self.name
references the NSString

1684
01:35:57,216 --> 01:36:00,446
that is my name and then
I'm going to compare

1685
01:36:00,836 --> 01:36:03,776
that like the NSString compare
method, I'm going to use

1686
01:36:03,776 --> 01:36:06,266
that to compare to the
other students name.

1687
01:36:08,336 --> 01:36:12,496
And because this compare method
returns an NSComparisonResults

1688
01:36:12,886 --> 01:36:14,106
that is an easy way
of making sure

1689
01:36:14,106 --> 01:36:15,916
that I return an
NSComparisonResult.

1690
01:36:17,126 --> 01:36:20,916
OK. So, now that I defined
this method for students,

1691
01:36:21,516 --> 01:36:28,226
I'm now going to say
here that I'm going

1692
01:36:28,226 --> 01:36:32,166
to sort the student's array
using the selector compare.

1693
01:36:32,536 --> 01:36:36,586
So this special syntax
@selector, remember that methods

1694
01:36:36,586 --> 01:36:39,956
and selectors are kind of like
interchangeable words almost.

1695
01:36:40,406 --> 01:36:45,716
So, I can't just say like this
'cause that's not really a

1696
01:36:45,716 --> 01:36:46,936
valid syntax.

1697
01:36:47,146 --> 01:36:50,396
I want to say that I want to
use the method compare colon,

1698
01:36:51,236 --> 01:36:55,856
and so to actually specify
the method compare colon,

1699
01:36:56,066 --> 01:36:58,966
I wrap it in this
special selector keyboard.

1700
01:36:59,466 --> 01:37:05,016
And so, what this sorted array
using selector method is going

1701
01:37:05,016 --> 01:37:10,316
to do is iterate over the
student's array calling the

1702
01:37:10,316 --> 01:37:13,796
compare method of each student
to say like, OK, student 0

1703
01:37:13,796 --> 01:37:15,726
and student 1, how do you
compare to each other?

1704
01:37:15,956 --> 01:37:18,256
And then like depending on
how they compare is going

1705
01:37:18,256 --> 01:37:20,376
to determine whether
student 0 belongs in front

1706
01:37:20,376 --> 01:37:22,566
of or after student 1.

1707
01:37:23,136 --> 01:37:26,086
So compare has to
exist for everything

1708
01:37:26,086 --> 01:37:27,096
in this student's array.

1709
01:37:27,676 --> 01:37:30,556
And then let's just run it

1710
01:37:30,556 --> 01:37:33,076
to make sure that's the
case that this works.

1711
01:37:33,876 --> 01:37:37,946
So we see a print Chris, David,
RJ, Rob which is what we wanted.

1712
01:37:38,516 --> 01:37:47,876
[ Inaudible Remark ]

1713
01:37:48,376 --> 01:37:51,446
>> So how do you in here--

1714
01:37:51,886 --> 01:37:57,996
and you can also decide that
we just want to compare ages.

1715
01:37:58,746 --> 01:38:03,656
This isn't quite right because
they're ints and I'm not sure

1716
01:38:03,656 --> 01:38:05,316
of what NSComparison looks like.

1717
01:38:06,136 --> 01:38:12,506
It might just be like
return Self.age less

1718
01:38:12,506 --> 01:38:14,566
than other Student.age,
or I guess maybe

1719
01:38:14,566 --> 01:38:16,996
like if 1 is less than,
I'm supposed to return 1.

1720
01:38:17,196 --> 01:38:20,356
Or if they're equal, I
return 0 or else negative 1.

1721
01:38:20,666 --> 01:38:22,966
I'm not sure what NSComparison,
what it was supposed

1722
01:38:22,966 --> 01:38:24,576
to look like actually.

1723
01:38:25,266 --> 01:38:30,486
So, another-- let's
go to here again.

1724
01:38:30,796 --> 01:38:35,426
So, a useful syntax that
we've already seen this useful

1725
01:38:35,426 --> 01:38:37,996
shorthand for defining an
NSString, that's blah, blah,

1726
01:38:37,996 --> 01:38:40,226
blah with the @ sign in
front of double quotes.

1727
01:38:40,866 --> 01:38:44,336
Another useful syntax is an
@ sign in front of brackets

1728
01:38:44,626 --> 01:38:52,576
for shorthand for defining
arrays, so and NSArrays.

1729
01:38:54,696 --> 01:38:59,246
So now I've defined an array,
an NS array with two objects,

1730
01:38:59,546 --> 01:39:01,376
Hello and World, two NSStrings.

1731
01:39:02,406 --> 01:39:06,356
And additional shorthand
that's somewhat is @

1732
01:39:06,596 --> 01:39:08,016
in front of any number.

1733
01:39:08,186 --> 01:39:12,906
So this defines the approach
like NSString was the first one,

1734
01:39:12,906 --> 01:39:13,986
NSArray was the second one.

1735
01:39:14,196 --> 01:39:17,356
This defines an NSNumber
with the value 42.

1736
01:39:18,226 --> 01:39:20,276
And so, what may
or may not work.

1737
01:39:20,476 --> 01:39:21,566
What I'm going to
show it right here.

1738
01:39:21,796 --> 01:39:25,766
No, actually that
won't work at all.

1739
01:39:25,886 --> 01:39:32,086
So, I might be able to
do init NSNumber alloc.

1740
01:39:32,276 --> 01:39:41,076
I just want to save the sense
of working at this point,

1741
01:39:41,076 --> 01:39:45,656
init with int self.age.

1742
01:39:46,116 --> 01:39:57,046
And then I want to compare that,
now I have a compare method.

1743
01:39:57,866 --> 01:40:03,656
Compare to another NSNumber
should be NSNumber alloc init

1744
01:40:04,086 --> 01:40:08,796
with int, other student, age.

1745
01:40:09,516 --> 01:40:13,136
[ Pause ]

1746
01:40:13,636 --> 01:40:17,266
OK. So questions on
what I just did there.

1747
01:40:17,586 --> 01:40:17,946
Yes.

1748
01:40:18,056 --> 01:40:20,116
>> You didn't use
the selector up here.

1749
01:40:20,706 --> 01:40:22,326
>> So, I'm not using-- here,

1750
01:40:22,326 --> 01:40:24,186
I'm just defining
the compare method.

1751
01:40:25,546 --> 01:40:28,506
It's when I want to actually
reference the compare method

1752
01:40:28,766 --> 01:40:34,536
that in here I say the method I
want to use is compare calling.

1753
01:40:34,536 --> 01:40:36,656
And this is again like I said
before, like the literal name

1754
01:40:36,656 --> 01:40:38,126
of the thing includes the colon.

1755
01:40:38,506 --> 01:40:41,036
So here I'm referencing
the method compare colon

1756
01:40:41,506 --> 01:40:43,956
and the definition of it, I
don't need to use @selector.

1757
01:40:45,516 --> 01:40:50,436
[ Inaudible Remark ]

1758
01:40:50,936 --> 01:40:52,686
A method to a method, yeah.

1759
01:40:54,006 --> 01:40:55,856
'Cause if it were a function,

1760
01:40:55,856 --> 01:40:57,186
I wouldn't need to
use that selector.

1761
01:40:57,186 --> 01:40:59,896
I could literally just
use a function pointer

1762
01:40:59,896 --> 01:41:01,436
but you don't need to
deal with those here.

1763
01:41:01,966 --> 01:41:08,436
There is some-- there
is probably some spacing

1764
01:41:08,436 --> 01:41:08,996
error here.

1765
01:41:08,996 --> 01:41:15,706
All right, 'cause I don't
need this, whatever.

1766
01:41:16,186 --> 01:41:18,896
That's the general idea.

1767
01:41:19,746 --> 01:41:23,266
So, that's that.

1768
01:41:24,236 --> 01:41:26,926
Questions before I move
on to a similar idea?

1769
01:41:27,066 --> 01:41:27,186
Yeah.

1770
01:41:28,516 --> 01:41:35,106
[ Inaudible Question ]

1771
01:41:35,606 --> 01:41:39,116
>> And just as-- remember,
it's like C where--

1772
01:41:40,336 --> 01:41:45,246
it's like C where this, in
order to get the prototype

1773
01:41:45,246 --> 01:41:49,466
for this thing, I can literally
just like copy this, paste it,

1774
01:41:49,466 --> 01:41:50,786
put a semicolon at the end.

1775
01:41:51,546 --> 01:41:53,446
So the prototype is
just the first line

1776
01:41:53,446 --> 01:41:59,496
of your definition
of the function.

1777
01:41:59,496 --> 01:42:03,686
OK. So, that's that
and now we're going

1778
01:42:03,686 --> 01:42:06,996
to students 9 'cause we're
getting into categories

1779
01:42:07,596 --> 01:42:21,236
which that's not very
helpful so here, let's-- OK.

1780
01:42:21,236 --> 01:42:25,256
So, the idea behind categories
is notice that in order

1781
01:42:25,256 --> 01:42:26,986
to do what I just did, I needed

1782
01:42:26,986 --> 01:42:31,736
to actively modify the
student class which is fine

1783
01:42:31,736 --> 01:42:35,566
because I'm the one who created
the student class so it's fine

1784
01:42:35,566 --> 01:42:37,936
for me to go into the student
class and add a compare method.

1785
01:42:38,906 --> 01:42:41,046
But, what if this
class that I need

1786
01:42:41,046 --> 01:42:43,326
to modify is not
something I have access to?

1787
01:42:43,606 --> 01:42:47,946
What if it's in some compiled
library or it's one of the--

1788
01:42:47,976 --> 01:42:50,246
one of the foundation
classes or an iOS class.

1789
01:42:50,816 --> 01:42:52,516
I need to add a method to it

1790
01:42:53,016 --> 01:42:55,156
but I don't have access
to the source code.

1791
01:42:55,756 --> 01:42:56,966
So these are what categories are

1792
01:42:57,066 --> 01:42:59,956
for therefore extending
existing classes.

1793
01:43:00,696 --> 01:43:04,796
And so, in Xcode you can
actually do this like file new,

1794
01:43:05,266 --> 01:43:08,006
file and then Category and then

1795
01:43:08,006 --> 01:43:09,346
like walkthrough
through the steps.

1796
01:43:09,836 --> 01:43:13,416
And the default format of
these categories, so remember,

1797
01:43:13,416 --> 01:43:14,526
I'm working under the assumption

1798
01:43:14,526 --> 01:43:16,306
that I don't have access
to the student class.

1799
01:43:17,046 --> 01:43:24,526
So the default format of
these filenames are student

1800
01:43:24,786 --> 01:43:27,736
which is the class that
I'm extending, plus,

1801
01:43:27,836 --> 01:43:29,976
and what I happen to
call the category.

1802
01:43:30,586 --> 01:43:35,356
So here, I am extending
the student class

1803
01:43:35,726 --> 01:43:39,766
with the category StudentCompare
and this is just the syntax

1804
01:43:39,766 --> 01:43:42,906
that you're free to look out
whenever you happen to need

1805
01:43:42,906 --> 01:43:44,166
to extend a class like this.

1806
01:43:44,986 --> 01:43:51,396
So, this extension of the
student class is going

1807
01:43:51,396 --> 01:43:53,116
to include this method.

1808
01:43:53,646 --> 01:43:58,506
A category is not able to
define instance variables,

1809
01:43:58,656 --> 01:44:02,066
it's only able to define
new methods for a class.

1810
01:44:02,306 --> 01:44:05,886
So, looking at the
implementation of this category,

1811
01:44:06,526 --> 01:44:10,336
again, it's-- the category
itself is StudentCompare

1812
01:44:10,586 --> 01:44:12,286
and I am extending student

1813
01:44:12,596 --> 01:44:15,536
and I'm implementing it
basically how I implemented it

1814
01:44:15,536 --> 01:44:18,506
before, exactly the same,
except I'm able to do this

1815
01:44:18,506 --> 01:44:21,196
without ever touching
the original Student.h

1816
01:44:21,196 --> 01:44:25,726
and Student.m. And this
will work exactly the same.

1817
01:44:25,726 --> 01:44:28,626
Now, in main.m, inside
of importing Student.h,

1818
01:44:28,626 --> 01:44:31,316
I import
Student+StudentCompare.h. The

1819
01:44:31,316 --> 01:44:34,476
StudentCompare.h is what
is importing Student.h.

1820
01:44:35,106 --> 01:44:41,236
And everything else
in here is the same.

1821
01:44:41,466 --> 01:44:41,916
Yes.

1822
01:44:42,516 --> 01:44:52,136
[ Inaudible Remark ]

1823
01:44:52,636 --> 01:44:53,976
>> Oh name, OK.

1824
01:44:54,271 --> 01:44:56,271
[ Inaudible Remark ]

1825
01:44:56,526 --> 01:44:59,846
>> Oh these angled
brackets and quotes.

1826
01:45:00,306 --> 01:45:06,886
So the angled brackets refer to
like these special directories

1827
01:45:06,966 --> 01:45:09,166
in the file system, in the
operating system that it knows

1828
01:45:09,166 --> 01:45:11,856
to look in to find these files.

1829
01:45:11,946 --> 01:45:15,306
So like Foundation/Foundation.h
is in one

1830
01:45:15,306 --> 01:45:16,446
such special directory.

1831
01:45:16,816 --> 01:45:20,586
Whereas these double quotes are
what you're going to want to use

1832
01:45:20,586 --> 01:45:22,716
to refer to files in
your current project.

1833
01:45:23,346 --> 01:45:27,976
So, in this project, I have a
Student+StudentCompare.h file.

1834
01:45:28,496 --> 01:45:30,176
Double quotes is like relative

1835
01:45:30,176 --> 01:45:33,566
to my current location whereas
angled brackets automatically

1836
01:45:33,566 --> 01:45:34,986
goes to these special
directories.

1837
01:45:35,886 --> 01:45:35,976
Yeah?

1838
01:45:36,516 --> 01:45:56,756
[ Inaudible Remark ]

1839
01:45:57,256 --> 01:45:59,806
You mean by addendum, do
you mean like this sort

1840
01:45:59,806 --> 01:46:00,976
of stuff, this category stuff?

1841
01:46:01,016 --> 01:46:02,076
[Inaudible Remark ]

1842
01:46:02,076 --> 01:46:03,066
Oh, then yes.

1843
01:46:03,066 --> 01:46:05,836
So, in student Compare.h, well,

1844
01:46:06,026 --> 01:46:08,406
let's say Student.h
was a foundation class.

1845
01:46:08,406 --> 01:46:08,686
>> Yeah, yeah.

1846
01:46:08,686 --> 01:46:11,906
>> Then I would do
foundation/Student.h.

1847
01:46:12,396 --> 01:46:15,976
And inside its main,
I would still do the

1848
01:46:15,976 --> 01:46:19,286
Student+StudentCompare.h. I
wouldn't import the original

1849
01:46:19,286 --> 01:46:20,506
foundation version of things.

1850
01:46:20,506 --> 01:46:22,166
I would import my
version of things

1851
01:46:22,166 --> 01:46:22,976
that have been overwritten.

1852
01:46:23,516 --> 01:46:32,146
[ Inaudible Remark ]

1853
01:46:32,646 --> 01:46:36,946
Yes, it's actually
just an extension.

1854
01:46:36,946 --> 01:46:41,206
It's the-- it is the same class.

1855
01:46:41,546 --> 01:46:43,496
And anywhere else in
your code that uses

1856
01:46:43,496 --> 01:46:46,146
that class could also
use that new method.

1857
01:46:47,466 --> 01:46:49,446
If it doesn't happen
to hush import,

1858
01:46:49,446 --> 01:46:52,196
the correct category file,
the compiler will complain

1859
01:46:52,196 --> 01:46:54,506
that like it doesn't
know that method exists.

1860
01:46:55,056 --> 01:46:59,486
But that method now actually
exists for that class.

1861
01:47:00,016 --> 01:47:02,016
[ Pause ]

1862
01:47:02,016 --> 01:47:03,000
[ Inaudible Remark ]

1863
01:47:03,256 --> 01:47:04,046
>> Mm-hmm.

1864
01:47:04,046 --> 01:47:07,296
>> Where is NSComparisonResult
coming from, is that a library?

1865
01:47:07,406 --> 01:47:10,456
>> So, that's something in
Foundation/Foundation.h,

1866
01:47:10,836 --> 01:47:14,456
we could actually just look
it up, NSComparisonResult.

1867
01:47:14,456 --> 01:47:21,456
So, something about the
Apple documentation is

1868
01:47:21,456 --> 01:47:23,596
that it can be overwhelming.

1869
01:47:23,796 --> 01:47:27,066
But it has a lot of good
examples so feel free

1870
01:47:27,066 --> 01:47:28,766
to ask questions on like
the message board if it--

1871
01:47:28,766 --> 01:47:30,616
you just find yourself
lost in a path

1872
01:47:30,616 --> 01:47:32,806
of various methods and stuff.

1873
01:47:33,526 --> 01:47:39,866
So, data types, what was I
was looking, NSComparison.

1874
01:47:40,376 --> 01:47:46,326
So, it looks like
NSComparisonResult is--

1875
01:47:46,666 --> 01:47:48,316
all right, this syntax,
you don't even know yet.

1876
01:47:48,596 --> 01:47:50,126
Let's hold on for that.

1877
01:47:50,676 --> 01:47:52,446
That's going to be
the next thing.

1878
01:47:52,976 --> 01:47:56,166
But NSComparisonResult
is just something

1879
01:47:56,166 --> 01:47:59,036
that foundation defines.

1880
01:47:59,036 --> 01:48:02,126
And that's why I wasn't 100
percent sure what it is.

1881
01:48:02,346 --> 01:48:06,016
I just know that the
compare methods of NSString

1882
01:48:06,016 --> 01:48:10,106
and NSNumber also a different
return NSComparisonResults

1883
01:48:10,296 --> 01:48:11,216
and so that's what I use.

1884
01:48:11,826 --> 01:48:15,986
But if you wanted, you
can construct your own

1885
01:48:16,106 --> 01:48:20,016
NSComparisonResult to return.

1886
01:48:20,016 --> 01:48:25,226
OK. Questions for
the next thing?

1887
01:48:25,386 --> 01:48:31,646
So, next thing is this.

1888
01:48:31,646 --> 01:48:33,146
Again, I think slide is
going to be worthless.

1889
01:48:34,566 --> 01:48:38,526
So blocks, so that's the
general format of blocks,

1890
01:48:38,526 --> 01:48:39,826
but that's worthless so--

1891
01:48:40,676 --> 01:48:42,386
looking at the example
of using a block.

1892
01:48:43,516 --> 01:48:56,036
[ Pause ]

1893
01:48:56,536 --> 01:48:59,406
OK. So, back to Student.h
and Student.m,

1894
01:49:00,146 --> 01:49:02,276
I have not modified them to
include the compare method.

1895
01:49:02,996 --> 01:49:07,276
I have not added a category
to extend the class.

1896
01:49:08,596 --> 01:49:11,286
Instead, I've used
what's called a block.

1897
01:49:11,686 --> 01:49:14,956
So a block is kind of
like an anonymous function

1898
01:49:14,956 --> 01:49:15,986
in other languages.

1899
01:49:15,986 --> 01:49:20,346
It's-- it doesn't even
have to be anonymous.

1900
01:49:20,346 --> 01:49:22,136
If you wanted, you could
assign it to a variable.

1901
01:49:22,336 --> 01:49:25,366
It's kind of like, if you're
familiar with the term,

1902
01:49:25,366 --> 01:49:29,846
it's like giving objective-C
first class functions to like

1903
01:49:30,356 --> 01:49:33,326
in objective-C, I'm not really
able to just define a function

1904
01:49:33,326 --> 01:49:35,016
in the middle of this method.

1905
01:49:35,016 --> 01:49:38,136
That's not entirely
valid, whereas,

1906
01:49:38,136 --> 01:49:39,716
I am able to define a block.

1907
01:49:40,296 --> 01:49:44,326
So, just like in JavaScript,
you saw a syntax like this.

1908
01:49:44,696 --> 01:49:48,616
Here, we have-- we are
using a different method.

1909
01:49:49,076 --> 01:49:52,296
So, before, we use sorted
array using selector.

1910
01:49:52,736 --> 01:49:54,886
Now, we're doing sorted
array using comparator.

1911
01:49:55,366 --> 01:49:58,346
And so, this comparator
expects a block

1912
01:49:59,286 --> 01:50:02,186
that this little
caret symbol specifies

1913
01:50:02,236 --> 01:50:04,646
that what is following
this is a block.

1914
01:50:06,056 --> 01:50:10,046
And this is the return type of
the block, NSComparisonResult.

1915
01:50:11,046 --> 01:50:16,576
This is the two-- now this is
the type of the first argument

1916
01:50:16,576 --> 01:50:17,776
and this is the type
of second argument.

1917
01:50:17,916 --> 01:50:20,056
And I know this because if
I look at the documentation

1918
01:50:20,056 --> 01:50:21,856
for sorted array
using comparator,

1919
01:50:22,256 --> 01:50:24,346
its argument is supposed
to be a block

1920
01:50:24,686 --> 01:50:26,526
that returns an
NSComparisonResult

1921
01:50:26,716 --> 01:50:28,646
and takes two arguments
that are both IDs.

1922
01:50:29,276 --> 01:50:32,186
So this, I got directly
from the documentation.

1923
01:50:33,266 --> 01:50:35,856
And then I need to
define the function itself

1924
01:50:35,856 --> 01:50:37,696
so I have my little curly brace.

1925
01:50:38,326 --> 01:50:44,026
And in here, I'm doing the same
sort of thing I did before.

1926
01:50:44,026 --> 01:50:48,686
We see here that I cast A
to student star and that's

1927
01:50:48,686 --> 01:50:52,216
because in here, A is just an
ID, in here, B is just an ID.

1928
01:50:52,606 --> 01:50:55,606
So if I actually want it to be
a student object, I first need

1929
01:50:55,606 --> 01:50:57,856
to cast it before or
passing it a name.

1930
01:50:58,806 --> 01:51:01,746
So Student star A name,
Student star B name,

1931
01:51:01,746 --> 01:51:02,696
gives me these two names

1932
01:51:02,696 --> 01:51:04,866
and then I use the
NSString's compare method.

1933
01:51:05,146 --> 01:51:08,366
So I don't have to construct
my own NSComparisonResult.

1934
01:51:09,986 --> 01:51:12,436
And we know that this
appropriately returns an

1935
01:51:12,466 --> 01:51:14,426
NSComparisonResult
like this at it should.

1936
01:51:15,886 --> 01:51:18,616
So blocks anonymous functions.

1937
01:51:18,856 --> 01:51:22,096
The syntax is a little
strange but it's convenient

1938
01:51:22,096 --> 01:51:24,766
that like I don't need
to define a method

1939
01:51:24,766 --> 01:51:28,556
in the student class
just to sort this array,

1940
01:51:29,466 --> 01:51:32,726
which like that was a very
heavy-handed result just

1941
01:51:32,726 --> 01:51:33,616
to get a sorted array.

1942
01:51:36,576 --> 01:51:36,776
[Inaudible Remark] Yes.

1943
01:51:37,076 --> 01:51:43,336
So that's where-- I don't
remember the syntax.

1944
01:51:43,336 --> 01:51:47,196
But it's going to look
something like-- so--

1945
01:51:48,516 --> 01:51:51,266
[ Pause ]

1946
01:51:51,766 --> 01:51:56,646
It's something like
what it returns,

1947
01:51:57,016 --> 01:52:03,986
then in parentheses what I want
to call it, then ID, ID equals.

1948
01:52:03,986 --> 01:52:06,786
And then pass it--

1949
01:52:07,516 --> 01:52:10,216
[ Pause ]

1950
01:52:10,716 --> 01:52:11,686
-- to here.

1951
01:52:14,996 --> 01:52:15,596
Didn't complain.

1952
01:52:16,576 --> 01:52:19,566
[laughs] So, if you're familiar
with function pointers in C,

1953
01:52:19,716 --> 01:52:21,256
I just sort of used
that same syntax.

1954
01:52:21,256 --> 01:52:25,916
I replaced the star
with a caret.

1955
01:52:26,336 --> 01:52:32,376
So, here is, we are defining
a variable called func

1956
01:52:33,656 --> 01:52:36,786
that is a block as
indicated by this caret.

1957
01:52:38,066 --> 01:52:40,366
This is-- the return type

1958
01:52:40,366 --> 01:52:42,326
of that block is
NSComparisonResult.

1959
01:52:42,636 --> 01:52:43,846
The arguments are two IDs.

1960
01:52:43,846 --> 01:52:46,756
And you're assigning
that to this block.

1961
01:52:47,456 --> 01:52:48,936
And then we're passing
that variable

1962
01:52:49,016 --> 01:52:52,106
to that function,
to that method.

1963
01:52:53,516 --> 01:52:57,836
[ Pause ]

1964
01:52:58,336 --> 01:52:58,666
Questions?

1965
01:52:59,516 --> 01:53:02,866
[ Pause ]

1966
01:53:03,366 --> 01:53:03,566
OK.

1967
01:53:04,516 --> 01:53:09,186
[ Pause ]

1968
01:53:09,686 --> 01:53:11,506
So couple last things.

1969
01:53:12,516 --> 01:53:20,186
[ Pause ]

1970
01:53:20,686 --> 01:53:22,116
OK, so protocols.

1971
01:53:22,116 --> 01:53:25,996
We're not going to go into
deep detail today about them.

1972
01:53:26,386 --> 01:53:31,686
But they are going to be pretty
substantial in your iOS usage.

1973
01:53:32,256 --> 01:53:37,806
So the syntax for this, remember
that well, in our interface,

1974
01:53:37,806 --> 01:53:39,496
we would also say
like :NSObject.

1975
01:53:39,696 --> 01:53:42,966
But here, this angle bracket
syntax is how we're going

1976
01:53:42,966 --> 01:53:46,156
to specify that it must
adhere to some protocol.

1977
01:53:46,846 --> 01:53:48,816
And so the NSCopying Protocol,

1978
01:53:49,126 --> 01:53:51,526
this is where I'm saying the
protocol is kind of interfaces

1979
01:53:51,526 --> 01:53:53,346
from other languages by saying

1980
01:53:53,346 --> 01:53:55,776
that the student
interface implements the

1981
01:53:55,776 --> 01:53:56,926
NSCopying Protocol.

1982
01:53:57,226 --> 01:53:59,416
I'm saying that it must
implement the methods

1983
01:53:59,416 --> 01:54:02,246
that NSCopying says, anything

1984
01:54:02,246 --> 01:54:03,796
that implements me
must implement.

1985
01:54:05,066 --> 01:54:07,336
So, looking at the
implementation now,

1986
01:54:09,116 --> 01:54:13,906
if we look at NSCopying, it says
that you must implement a copy

1987
01:54:13,906 --> 01:54:15,876
with zone function method.

1988
01:54:16,426 --> 01:54:18,016
And so here's us
implementing it.

1989
01:54:18,016 --> 01:54:20,606
You don't really have to care
about the syntax of it for now

1990
01:54:20,606 --> 01:54:22,096
or the-- what it
actually is doing.

1991
01:54:22,486 --> 01:54:26,096
Just the idea that since
student implements the NSCopying

1992
01:54:26,096 --> 01:54:29,286
Protocol, it must
implement all the function,

1993
01:54:29,386 --> 01:54:31,336
all the methods that
NSCopying says.

1994
01:54:31,896 --> 01:54:40,766
And the use of this as we
see is going to be like a lot

1995
01:54:40,766 --> 01:54:42,226
of these functions that--

1996
01:54:42,226 --> 01:54:45,546
or a lot of like classes
that you're going to be using

1997
01:54:45,546 --> 01:54:48,266
with iOS, like a lot of
these drawing classes,

1998
01:54:48,666 --> 01:54:50,986
are going to expect
you to pass it objects

1999
01:54:51,186 --> 01:54:52,856
that must adhere to
certain protocols.

2000
01:54:53,466 --> 01:54:56,566
For example, UITableView is one.

2001
01:54:56,836 --> 01:54:58,766
Let's actually looked
at documentation,

2002
01:55:00,266 --> 01:55:01,946
this documentation work.

2003
01:55:02,516 --> 01:55:13,056
[ Pause ]

2004
01:55:13,556 --> 01:55:16,766
OK. So we see in
the documentation

2005
01:55:17,046 --> 01:55:20,056
that it looks like
a data source.

2006
01:55:20,126 --> 01:55:25,006
So, UITableView is,
in like your iPhone,

2007
01:55:25,006 --> 01:55:27,096
it would be like your
contact list sort of,

2008
01:55:27,096 --> 01:55:29,786
it's just like a bunch
of rows with data in them

2009
01:55:29,786 --> 01:55:31,046
and you can scroll
up and down on it.

2010
01:55:31,676 --> 01:55:37,796
So here, UITableView defines
a data source property

2011
01:55:38,426 --> 01:55:41,826
where the type of that
data source is just an ID

2012
01:55:42,176 --> 01:55:44,586
that implements UITableView
data source.

2013
01:55:45,496 --> 01:55:49,466
And so this is important because
if you then set this property

2014
01:55:49,466 --> 01:55:52,206
to be some object, that means

2015
01:55:52,206 --> 01:55:55,126
that that object must
implement this protocol,

2016
01:55:55,656 --> 01:55:59,106
what this protocol says
must be implemented are--

2017
01:55:59,696 --> 01:56:02,036
so some methods are
optional, some are required,

2018
01:56:02,196 --> 01:56:03,746
so these two methods
are required.

2019
01:56:04,226 --> 01:56:08,026
And so, when you set that
property of a UITableView

2020
01:56:08,026 --> 01:56:11,786
with some object, that object
must have these two methods.

2021
01:56:12,096 --> 01:56:16,916
Let's say, like how many rows
there should be in this table,

2022
01:56:17,196 --> 01:56:19,746
in this section, and
also what should go

2023
01:56:19,746 --> 01:56:20,906
in each particular cell.

2024
01:56:21,726 --> 01:56:24,676
So when you try to
create a TableView,

2025
01:56:24,676 --> 01:56:28,166
you need to pass some
object that is capable

2026
01:56:28,166 --> 01:56:30,586
of populating the cells
of that TableView.

2027
01:56:31,136 --> 01:56:37,476
And so, UITableView knows
that you are capable of doing

2028
01:56:37,476 --> 01:56:42,096
that if you implement
the protocol.

2029
01:56:42,416 --> 01:56:44,556
And you'll see that that
sort of pattern is going

2030
01:56:44,556 --> 01:56:46,296
to be pretty common with
a lot of things you do.

2031
01:56:49,716 --> 01:56:50,976
Questions with protocols?

2032
01:56:51,516 --> 01:56:55,476
[ Pause ]

2033
01:56:55,976 --> 01:56:57,146
And that's really why I like it.

2034
01:56:57,146 --> 01:56:58,986
You compare to like
a Java interface.

2035
01:56:59,276 --> 01:57:01,006
Protocols are much
more similar to those

2036
01:57:01,006 --> 01:57:05,516
since like protocols almost
define a new type kind of,

2037
01:57:05,856 --> 01:57:10,036
whereas like an interface in
Objective-C is just a list

2038
01:57:10,036 --> 01:57:11,566
of all the methods
that you implement.

2039
01:57:12,116 --> 01:57:13,576
It doesn't say anything
intelligent

2040
01:57:13,576 --> 01:57:19,216
about like, "OK,
you're welcome."

2041
01:57:19,456 --> 01:57:23,696
Really, what protocols allow are
like multiple objects to all--

2042
01:57:23,696 --> 01:57:27,646
multiple classes to all
fulfill some common purpose.

2043
01:57:28,116 --> 01:57:31,766
So as long as like-- an
example are all these

2044
01:57:31,766 --> 01:57:35,006
like enumeration classes, the
collection classes, NSArray,

2045
01:57:35,006 --> 01:57:36,656
and NSDictionary, and all those,

2046
01:57:37,136 --> 01:57:39,936
as long as you appropriately
implement some protocol,

2047
01:57:40,176 --> 01:57:43,106
then you are able to use
the fast enumeration syntax

2048
01:57:43,176 --> 01:57:46,846
on that object, that
for...in syntax.

2049
01:57:47,376 --> 01:57:52,046
And, again, much more than that.

2050
01:57:52,046 --> 01:57:53,856
All right.

2051
01:57:53,856 --> 01:57:55,846
Just-- so you've seen it.

2052
01:57:56,436 --> 01:57:59,716
NSExceptions is very similar to
some other languages, the try

2053
01:57:59,716 --> 01:58:02,166
and catch blocks and we
also happen to have finally.

2054
01:58:02,606 --> 01:58:07,656
So that's just to
know it exists.

2055
01:58:09,146 --> 01:58:11,256
You probably won't even
use them that much,

2056
01:58:11,446 --> 01:58:15,216
but that's useful to know.

2057
01:58:15,336 --> 01:58:17,326
NSError is somewhat
of an alternative.

2058
01:58:17,326 --> 01:58:19,936
So, instead of having to use

2059
01:58:19,936 --> 01:58:23,676
like the try-catch-finally
syntax can be somewhat large,

2060
01:58:23,676 --> 01:58:27,236
especially if like each one
of those do something here,

2061
01:58:27,236 --> 01:58:31,696
just like a single line then
you've sort of like wrapped it

2062
01:58:31,696 --> 01:58:34,966
with three times as many lines
are accomplishing something.

2063
01:58:35,856 --> 01:58:39,486
So, NS-- or well,
this sort of syntax,

2064
01:58:40,056 --> 01:58:41,726
this is one of the
rare scenarios

2065
01:58:41,726 --> 01:58:45,296
where are you are going to be
using ampersand still, so--

2066
01:58:45,346 --> 01:58:47,666
again, we're never
dereferencing an object,

2067
01:58:48,606 --> 01:58:52,636
but we are passing the
address of an object.

2068
01:58:53,306 --> 01:58:56,916
We are passing the address
of this NSError so that

2069
01:58:56,986 --> 01:59:01,476
if something happens to go wrong
when we pass the method bar--

2070
01:59:01,716 --> 01:59:03,296
the method is called bar:error:.

2071
01:59:03,296 --> 01:59:07,366
So when we pass the
method bar:error: foo,

2072
01:59:08,036 --> 01:59:09,836
if something happens
to go wrong,

2073
01:59:10,246 --> 01:59:16,856
then foo will instantiate
an NSError object and set E

2074
01:59:17,066 --> 01:59:18,546
to be that NSError object.

2075
01:59:19,126 --> 01:59:23,326
So, inside of here, if something
went wrong, E will not be nil.

2076
01:59:24,246 --> 01:59:29,776
If we just pass E, then inside
of this function, this method,

2077
01:59:29,776 --> 01:59:31,616
it would have to dereference E

2078
01:59:31,696 --> 01:59:32,846
which is something you
shouldn't be doing.

2079
01:59:32,846 --> 01:59:34,926
You should never
dereference an object.

2080
01:59:35,896 --> 01:59:38,796
Plus, if you try to dereference
E, you'd be dereferencing nil.

2081
01:59:40,516 --> 01:59:46,276
[ Inaudible Remark ]

2082
01:59:46,776 --> 01:59:50,846
So it will-- by returning
nil, it's indicating

2083
01:59:50,846 --> 01:59:54,126
that something went wrong
and then inside of here, now,

2084
01:59:54,126 --> 01:59:56,936
since something must have
gone wrong, E must be set

2085
01:59:56,936 --> 01:59:57,926
to something that isn't nil.

2086
01:59:58,296 --> 02:00:01,376
Inside of the function, it must
have instantiated an NSError

2087
02:00:01,376 --> 02:00:07,746
object and did like star-- star
E equals this NSError object.

2088
02:00:08,516 --> 02:00:19,436
[ Inaudible Remark ]

2089
02:00:19,936 --> 02:00:21,816
Well, it's-- well, the function

2090
02:00:21,816 --> 02:00:24,396
or the method is bar
error, bar:error:.

2091
02:00:25,066 --> 02:00:26,616
But if something
goes wrong, yeah.

2092
02:00:26,616 --> 02:00:28,776
It-- that's just like when
you read the documentation,

2093
02:00:28,776 --> 02:00:32,166
it will say like, "If something
goes wrong, nil will be returned

2094
02:00:32,166 --> 02:00:34,866
and error will be
set to some NSError."

2095
02:00:35,496 --> 02:00:40,446
So, it's also possible that it
will return void and then it's

2096
02:00:40,446 --> 02:00:42,956
up to you just to
see if E is still nil

2097
02:00:43,136 --> 02:00:44,526
and that's the way you
know there was an error.

2098
02:00:45,296 --> 02:00:49,296
But, that's just-- in this
example, we say, nil is returned

2099
02:00:49,296 --> 02:00:52,866
on error and what that
error was is inside

2100
02:00:53,946 --> 02:00:57,176
of E. [Inaudible
Remark] Yeah, it's not.

2101
02:00:57,336 --> 02:01:01,236
So, instead of just failing,
we just put it inside of E.

2102
02:01:04,196 --> 02:01:06,496
But other things do use
the try-catch syntax

2103
02:01:06,916 --> 02:01:09,086
so some things don't have this.

2104
02:01:10,916 --> 02:01:12,496
All right.

2105
02:01:13,386 --> 02:01:13,976
Any other questions?

2106
02:01:14,516 --> 02:01:17,276
[ Pause ]

2107
02:01:17,776 --> 02:01:20,626
I think that's it for
our Objective-C intro.

2108
02:01:20,986 --> 02:01:25,346
If there is anything
left, it was a lot,

2109
02:01:25,346 --> 02:01:30,226
but we haven't really done any
iOS stuff so we'll throw all

2110
02:01:30,226 --> 02:01:30,986
that at you next week.

