1
00:00:00,506 --> 00:00:06,716
[ Silence ]

2
00:00:07,216 --> 00:00:10,766
>> OK, welcome back to S-76,
so tonight we actually dive

3
00:00:10,766 --> 00:00:13,786
in to iOS programming, building
on the material that you dove

4
00:00:13,786 --> 00:00:15,236
into with Rob last week.

5
00:00:15,236 --> 00:00:17,166
And what was that language

6
00:00:17,636 --> 00:00:20,526
that we spent the latter
half of last week on?

7
00:00:20,676 --> 00:00:21,276
>> Objective-C.

8
00:00:21,466 --> 00:00:23,046
>> Objective-C, exactly.

9
00:00:23,046 --> 00:00:25,726
So, let's see if we can take
a whirlwind tour of some

10
00:00:25,726 --> 00:00:28,886
of those topics and see
if this will lend itself

11
00:00:28,886 --> 00:00:31,886
to recognizing some of the same
constructs you saw last week

12
00:00:32,086 --> 00:00:35,256
albeit in a different context
namely GUI programming today.

13
00:00:35,526 --> 00:00:39,806
So, here was a canonical
program of main.m. Dot M,

14
00:00:39,806 --> 00:00:43,616
that was probably a new file
extension for some folks.

15
00:00:43,976 --> 00:00:45,916
What's the dot M referring to?

16
00:00:46,496 --> 00:00:47,196
>> Refer to methods?

17
00:00:47,346 --> 00:00:48,706
>> So, methods typically.

18
00:00:48,706 --> 00:00:51,196
So, methods are where you
actually implement your

19
00:00:51,196 --> 00:00:52,516
functions or your
methods and this is

20
00:00:52,516 --> 00:00:54,176
in contrast with
the dot H files.

21
00:00:54,176 --> 00:00:56,536
And the dot H file in
Objective-C contained what kind

22
00:00:56,536 --> 00:00:57,596
of things by contrast?

23
00:00:58,186 --> 00:00:58,416
>> Interface.

24
00:00:58,816 --> 00:01:01,956
>> So, the interface or
the sort of the information

25
00:01:01,956 --> 00:01:05,206
that describes what the code is
that you're about to implement,

26
00:01:05,206 --> 00:01:07,636
and this is an example
that we'll see pretty much

27
00:01:07,636 --> 00:01:10,446
in every iOS application
that we make, certainly ones

28
00:01:10,656 --> 00:01:13,466
that we make by starting,
from some of Apple's templates

29
00:01:13,706 --> 00:01:16,696
and it is-- I cannot
think of a single instance

30
00:01:16,696 --> 00:01:17,776
for the next four weeks

31
00:01:17,776 --> 00:01:20,056
where you should ever
actually touch this file.

32
00:01:20,056 --> 00:01:21,326
So, you can take
comfort in the fact

33
00:01:21,326 --> 00:01:23,446
that the way you get it is
the way it should remain

34
00:01:23,766 --> 00:01:27,306
but it actually is the file that
kick starts a lot of the magic

35
00:01:27,306 --> 00:01:29,026
that then proceeds to happen,

36
00:01:29,026 --> 00:01:30,776
and we'll start giving
you a tour of that today.

37
00:01:30,776 --> 00:01:33,486
So, let's work through this
line by line as representative

38
00:01:33,486 --> 00:01:34,886
of some of the syntax
we'll continue

39
00:01:34,886 --> 00:01:36,996
to see even though it won't
be in this particular file.

40
00:01:36,996 --> 00:01:39,226
So, slash-- hash import.

41
00:01:39,736 --> 00:01:42,856
What does that do for us?

42
00:01:42,856 --> 00:01:45,456
First line in a file like this?

43
00:01:45,596 --> 00:01:45,663
Yeah?

44
00:01:45,663 --> 00:01:46,406
>> Import the library.

45
00:01:46,956 --> 00:01:48,256
>> Yeah, it imports the library.

46
00:01:48,256 --> 00:01:51,116
Specifically the library that's
called the foundation library

47
00:01:51,116 --> 00:01:53,776
which for Objective-C is all
the basic stuff that you get

48
00:01:53,776 --> 00:01:56,696
with Objective-C things
like arrays and other types

49
00:01:56,696 --> 00:02:00,516
of objects, sort of like Java's
collection classes and the like,

50
00:02:00,706 --> 00:02:03,646
so-- or things you might get
in C++ is standard library.

51
00:02:03,646 --> 00:02:05,556
So, a lot of functionality
right out of the box

52
00:02:05,756 --> 00:02:08,206
and a hashtag import means

53
00:02:08,206 --> 00:02:12,236
that those method declarations
will be automatically loaded

54
00:02:12,236 --> 00:02:14,376
into this file so you can just
start using them even though

55
00:02:14,376 --> 00:02:17,246
someone else implemented those
functions and those classes.

56
00:02:17,586 --> 00:02:18,886
So, int main just means

57
00:02:18,886 --> 00:02:20,396
that this function
returns an Interviewer.

58
00:02:20,396 --> 00:02:22,846
For our purposes that will
pretty much be inconsequential,

59
00:02:22,846 --> 00:02:23,756
but this is one of the means

60
00:02:23,756 --> 00:02:27,126
by which programs can signal
whether they were successful

61
00:02:27,126 --> 00:02:27,666
or not.

62
00:02:27,666 --> 00:02:31,416
If a program like main returns
0 that means all is well.

63
00:02:31,446 --> 00:02:33,346
If it returns anything
other than 0,

64
00:02:33,346 --> 00:02:34,526
what does that typically
signify?

65
00:02:35,116 --> 00:02:38,666
All is not well, and there can
be almost an infinite number

66
00:02:38,666 --> 00:02:41,126
of things that could go wrong
and that's why we use 0 for good

67
00:02:41,126 --> 00:02:43,526
and unknown 0 value
for anything else.

68
00:02:43,526 --> 00:02:47,026
Argc and argv typically refer
to command line arguments,

69
00:02:47,026 --> 00:02:49,076
things you would actually
provide to a program

70
00:02:49,076 --> 00:02:50,566
by typing commands
at a keyboard.

71
00:02:50,566 --> 00:02:51,786
That's not going
to be applicable

72
00:02:51,786 --> 00:02:53,346
for our iOS programming.

73
00:02:53,656 --> 00:02:56,606
And then inside here we
see some syntax that's new

74
00:02:56,606 --> 00:02:59,526
to Objective-C, most everything
else we have seen thus far is

75
00:02:59,896 --> 00:03:03,816
exists in C or in
a form similar to C

76
00:03:03,966 --> 00:03:07,026
and autorelease pool
refers to memory management.

77
00:03:07,026 --> 00:03:10,446
And we'll come back to this
over time but in recent version

78
00:03:10,446 --> 00:03:13,566
of iOS, a whole bunch of useful
functionality was introduced,

79
00:03:13,836 --> 00:03:15,786
that essentially means
that we can start thinking

80
00:03:15,786 --> 00:03:17,756
about memory a lot less.

81
00:03:17,756 --> 00:03:20,266
And I'll point out for
here and there exactly

82
00:03:20,266 --> 00:03:22,676
where there is interesting
memory management going on

83
00:03:22,676 --> 00:03:26,266
but know that two years ago had
you taken this class your lives

84
00:03:26,266 --> 00:03:28,186
would have been much more
challenging when it comes

85
00:03:28,186 --> 00:03:30,406
to implementing some of the
stuff we'll dive into today.

86
00:03:30,626 --> 00:03:33,286
But Apple has lowered
the bar to getting

87
00:03:33,286 --> 00:03:36,146
into Objective-C programming
in part by hiding some

88
00:03:36,146 --> 00:03:38,816
of the complexity of
memory management.

89
00:03:38,816 --> 00:03:41,086
NSLog is just a function
like printf

90
00:03:41,136 --> 00:03:44,666
that will print a formatted
string but to a specific place,

91
00:03:44,666 --> 00:03:48,266
a standard logging mechanism
that we'll actually see in Xcode

92
00:03:48,266 --> 00:03:51,466
which recalls the IDE we'll
use to program in Objective-C.

93
00:03:51,696 --> 00:03:54,496
Return 0 means that we're
assuming all is going to go well

94
00:03:54,636 --> 00:03:58,156
and we will pretty much never
see this function again.

95
00:03:59,646 --> 00:04:00,456
Yes, questions.

96
00:04:00,546 --> 00:04:03,376
>> Should we start every
program with autorelease?

97
00:04:03,466 --> 00:04:03,936
>> Good question.

98
00:04:03,936 --> 00:04:06,246
Should you start every
program with autorelease pool?

99
00:04:06,246 --> 00:04:09,666
Short answer yes but objective--

100
00:04:09,666 --> 00:04:12,306
Xcode will actually
write this code for you.

101
00:04:12,306 --> 00:04:15,256
So, we'll actually see in a
little bit a function quite

102
00:04:15,256 --> 00:04:17,036
like this but with
one additional line

103
00:04:17,036 --> 00:04:21,556
that kicks off the magic
that is the SDK that comes

104
00:04:21,556 --> 00:04:23,636
with Xcode for iOS programming.

105
00:04:23,636 --> 00:04:25,866
So, you can [inaudible]
literally just take what's given

106
00:04:25,866 --> 00:04:26,986
to you out of the box.

107
00:04:27,386 --> 00:04:29,796
All right, so, here is some
syntax, and just to be clear,

108
00:04:29,796 --> 00:04:32,816
I've gone into Rob's
slides and I've deleted some

109
00:04:32,816 --> 00:04:35,466
of the comments so that we
can put them back to make sure

110
00:04:35,466 --> 00:04:37,526
that we're all in the same
page before we forge ahead.

111
00:04:37,776 --> 00:04:39,356
So, this is what you might see

112
00:04:39,356 --> 00:04:41,526
in a dot H file or
a header file.

113
00:04:41,716 --> 00:04:44,926
You said a moment ago that this
is where declarations of methods

114
00:04:44,926 --> 00:04:46,666
and other-- and classes might go

115
00:04:46,666 --> 00:04:48,296
but not the actual
implementations.

116
00:04:48,626 --> 00:04:50,716
I have two comment symbols here.

117
00:04:50,976 --> 00:04:54,596
What goes where that first
comment symbol is specifically?

118
00:04:54,946 --> 00:04:57,786
What do you put inside of
an interface declaration?

119
00:04:58,316 --> 00:05:00,556
So, instance variables?

120
00:05:00,556 --> 00:05:02,806
And what's the definition of
an instance variable, to recap?

121
00:05:03,516 --> 00:05:06,516
[ Pause ]

122
00:05:07,016 --> 00:05:08,000
[ Inaudible Remark ]

123
00:05:09,646 --> 00:05:11,956
OK, almost the circular
definition here.

124
00:05:11,956 --> 00:05:12,546
What's an instance?

125
00:05:12,546 --> 00:05:14,086
Instance of what?

126
00:05:15,316 --> 00:05:17,766
>> An instance of a class.

127
00:05:17,766 --> 00:05:21,536
>> An instance of a class
otherwise known as an object.

128
00:05:21,706 --> 00:05:24,676
So, an instance is just an
object, an instantiation

129
00:05:24,676 --> 00:05:25,546
of a particular class.

130
00:05:25,916 --> 00:05:29,586
An instance variable is a piece
of data, maybe it's 8 bits,

131
00:05:29,586 --> 00:05:31,406
maybe it's 32 bits,
maybe it's even bigger,

132
00:05:31,616 --> 00:05:34,526
that's specifically associated
with the specific object

133
00:05:34,886 --> 00:05:36,926
that was created
using some class

134
00:05:37,256 --> 00:05:40,266
as its blueprint, so to speak.

135
00:05:40,266 --> 00:05:42,536
So, this is where our
instance variables might go.

136
00:05:42,746 --> 00:05:45,386
And how about outside
of the curly braces

137
00:05:45,386 --> 00:05:47,326
but before the @end symbol,

138
00:05:47,326 --> 00:05:49,526
what goes where those
second comment marks are?

139
00:05:50,006 --> 00:05:52,636
>> [Inaudible] class methods?

140
00:05:52,736 --> 00:05:53,996
>> Class methods, sure.

141
00:05:53,996 --> 00:05:57,486
So we can declare class methods
there, not implement them

142
00:05:57,546 --> 00:06:00,656
but declare them, which means,
hey compiler, here is the name

143
00:06:00,656 --> 00:06:03,786
and the signature, all of the
arguments for a method I'm going

144
00:06:03,786 --> 00:06:07,196
to implement elsewhere,
elsewhere being the dot M file.

145
00:06:07,366 --> 00:06:09,366
And what else besides
class methods can go

146
00:06:09,366 --> 00:06:11,586
where that second
comment symbol is?

147
00:06:12,236 --> 00:06:13,576
Instance method.

148
00:06:13,576 --> 00:06:16,606
So, instance methods or sort of
the opposite of class methods

149
00:06:16,606 --> 00:06:21,126
in the sense that a class method
can be called even before you--

150
00:06:22,536 --> 00:06:22,876
>> Instantiate.

151
00:06:22,876 --> 00:06:24,106
>> Instantiate an object.

152
00:06:24,106 --> 00:06:25,716
So, if you just know
a classes' name,

153
00:06:25,866 --> 00:06:27,716
you've never actually
used it as the blueprint

154
00:06:27,716 --> 00:06:31,156
for creating an object, you
can call on class method

155
00:06:31,456 --> 00:06:33,826
on that class without
an object existing.

156
00:06:33,826 --> 00:06:37,686
And this is hugely useful 'cause
you saw one at least last week

157
00:06:37,816 --> 00:06:39,926
that allows us to avoid what
would otherwise be a chicken

158
00:06:39,926 --> 00:06:40,776
in the egg problem.

159
00:06:40,946 --> 00:06:43,966
If we didn't have class
methods, we could never allocate

160
00:06:43,966 --> 00:06:47,176
or alloc an object of
a particular class,

161
00:06:47,266 --> 00:06:50,116
and that was the plus alloc that
you might recall from last week,

162
00:06:50,116 --> 00:06:51,396
and we'll continue
to see tonight.

163
00:06:51,396 --> 00:06:54,486
An instance method by contrast
is one that you call on in--

164
00:06:55,016 --> 00:06:58,766
on an object or particular
instance of a class.

165
00:06:58,926 --> 00:07:00,526
Exactly. So, we'll start
seeing a lot of this

166
00:07:00,526 --> 00:07:03,516
and we'll start writing our
own classes in this way.

167
00:07:03,616 --> 00:07:06,266
But there's one last syntactic
detail that's worth recapping

168
00:07:06,266 --> 00:07:11,786
here, the colon space NSObject
means what, with respect to Foo?

169
00:07:12,016 --> 00:07:13,816
[ Inaudible Remark ]

170
00:07:13,816 --> 00:07:16,956
It inherits from
the NSObject class.

171
00:07:16,956 --> 00:07:19,516
And most classes that we
would ever write, most classes

172
00:07:19,516 --> 00:07:22,886
that you'll use in the SDK that
Apple provides due at some point

173
00:07:22,886 --> 00:07:25,616
to send from NSObject even
though there's typically even

174
00:07:25,616 --> 00:07:30,176
more layers than just one
between you and that root class.

175
00:07:30,436 --> 00:07:32,326
So, we'll see more of
that in the time to come.

176
00:07:32,326 --> 00:07:34,576
So, instance variables and
declarations of methods.

177
00:07:34,576 --> 00:07:35,826
And these slides
are online if you'd

178
00:07:35,826 --> 00:07:37,746
like to download them as usual.

179
00:07:38,056 --> 00:07:41,686
Dot M files contain
implementations, and this is one

180
00:07:41,686 --> 00:07:44,646
of the sort of unfortunate
design decisions of Objective-C

181
00:07:44,646 --> 00:07:46,266
that you may recall last week.

182
00:07:46,266 --> 00:07:50,106
Even though you do have balanced
curly braces in a lot of places,

183
00:07:50,406 --> 00:07:54,366
you also have wonderfully
asymmetric ways of starting

184
00:07:54,486 --> 00:07:57,646
and ending a sentence,
so to speak.

185
00:07:57,646 --> 00:08:00,616
You have @implementation and
then @end which is the opposite.

186
00:08:00,616 --> 00:08:01,816
But we saw this a moment ago.

187
00:08:01,926 --> 00:08:04,846
What goes where that
comment symbol is?

188
00:08:06,016 --> 00:08:07,676
[ Inaudible Remark ]

189
00:08:07,676 --> 00:08:08,716
The definitions

190
00:08:08,716 --> 00:08:10,846
or the implementations
of those methods.

191
00:08:10,846 --> 00:08:12,766
So, you typically
begin by copying

192
00:08:12,766 --> 00:08:16,016
and pasting the declaration or
the signature of your methods

193
00:08:16,016 --> 00:08:19,386
from the dot H file but then you
proceed to open a curly brace,

194
00:08:19,676 --> 00:08:21,876
implement it and then
close a curly brace,

195
00:08:21,876 --> 00:08:24,036
and we'll certainly do
that tonight as well.

196
00:08:24,036 --> 00:08:25,966
So, there go our
definitions of methods.

197
00:08:26,376 --> 00:08:28,056
All right, so now
a bit more syntax.

198
00:08:28,056 --> 00:08:32,246
Messages we looked at last week,
and this is a valid line of code

199
00:08:32,246 --> 00:08:35,726
that assumes that a class
called student already exists

200
00:08:35,726 --> 00:08:37,676
and has been declared
and defined elsewhere.

201
00:08:37,996 --> 00:08:39,936
It could someone in--
somewhat technical

202
00:08:39,936 --> 00:08:43,196
but roughly layman's terms
explain what the two halves

203
00:08:43,356 --> 00:08:45,096
of this line of code are doing.

204
00:08:45,766 --> 00:08:49,126
Let's start with the side on
the left of the equal sign.

205
00:08:49,976 --> 00:08:52,156
What's going on with
those symbols?

206
00:08:52,156 --> 00:08:55,746
Students star student.

207
00:08:56,406 --> 00:08:56,496
Yeah.

208
00:08:57,516 --> 00:09:01,816
[ Inaudible Remark ]

209
00:09:02,316 --> 00:09:05,576
Creating an object with
a pointer called student.

210
00:09:05,576 --> 00:09:08,106
Close but we're not
doing as much

211
00:09:08,106 --> 00:09:09,456
as you just proposed
that we're doing.

212
00:09:09,656 --> 00:09:10,746
We're doing a little less.

213
00:09:11,586 --> 00:09:11,716
Yeah?

214
00:09:12,516 --> 00:09:16,916
[ Inaudible Remark ]

215
00:09:17,416 --> 00:09:19,406
Again, I'm going to define fault

216
00:09:19,406 --> 00:09:21,466
with your use of
create an object.

217
00:09:22,416 --> 00:09:22,536
Yeah?

218
00:09:23,516 --> 00:09:26,036
[ Inaudible Speaker ]

219
00:09:26,536 --> 00:09:31,116
Exactly. Perfect so, create a
pointer which is just storage

220
00:09:31,116 --> 00:09:33,806
for an address and the name of
that pointer is apparently going

221
00:09:33,806 --> 00:09:37,556
to be student in lower case
and that pointer is going

222
00:09:37,556 --> 00:09:41,026
to point to, in other words,
store the address of an object

223
00:09:41,026 --> 00:09:44,956
of type Student capital S.
So, all that is happening

224
00:09:44,956 --> 00:09:46,786
on the left hand side
of that screen is

225
00:09:46,786 --> 00:09:49,996
that we're allocating
probably 64 bits or 8 bytes

226
00:09:50,226 --> 00:09:53,356
to store the address of what
will be a student object.

227
00:09:53,356 --> 00:09:55,146
But at this point in
the story, only looking

228
00:09:55,146 --> 00:09:57,186
at the left hand side
of that line of code,

229
00:09:57,366 --> 00:10:00,016
none of the object
instantiation has happened yet.

230
00:10:00,016 --> 00:10:02,726
All we've done is created
an empty place holder

231
00:10:02,726 --> 00:10:05,756
for what will be the address
of some student object.

232
00:10:06,246 --> 00:10:09,316
So now we can rewind and go
with the right hand side.

233
00:10:09,316 --> 00:10:10,976
What's happening on the right
hand side of the line of code?

234
00:10:11,516 --> 00:10:13,716
[ Inaudible Remark ]

235
00:10:14,216 --> 00:10:17,336
Exactly. On the right hand side
of the code we are allocating,

236
00:10:17,386 --> 00:10:20,546
in other words instantiating
an object of type student.

237
00:10:20,906 --> 00:10:24,356
And what that syntax with the
square brackets is presumably

238
00:10:24,356 --> 00:10:26,706
doing is somehow returning what

239
00:10:26,706 --> 00:10:28,836
to the left hand
side for assignment?

240
00:10:30,376 --> 00:10:30,746
The address.

241
00:10:31,116 --> 00:10:34,656
So alloc is apparently not
only allocating the object,

242
00:10:34,656 --> 00:10:37,926
it is allocating it and then
handing back the address,

243
00:10:38,006 --> 00:10:40,186
something like one, two,
three, wherever it is in RAM,

244
00:10:40,456 --> 00:10:42,396
and so that known
value is what's gets--

245
00:10:42,396 --> 00:10:44,906
or getting stored in the
left hand side which is

246
00:10:44,906 --> 00:10:47,276
that 64 bit chunk
of memory designed

247
00:10:47,276 --> 00:10:49,216
to hold the address
of an object.

248
00:10:49,216 --> 00:10:50,626
A hand, OK, Peter.

249
00:10:50,626 --> 00:10:52,156
>> So, without calling init,

250
00:10:52,376 --> 00:10:55,476
is it actually an
instantiated object

251
00:10:55,476 --> 00:10:59,356
or is it just the added
memory has been allocated?

252
00:10:59,606 --> 00:11:00,886
>> Good question,
just the latter.

253
00:11:00,886 --> 00:11:03,596
So when you call alloc, all
you have done is allocated the

254
00:11:03,596 --> 00:11:06,376
object and therefore
returning its address.

255
00:11:06,626 --> 00:11:08,186
You have not initialized it.

256
00:11:08,476 --> 00:11:11,806
But in many cases,
initializing it doesn't have an

257
00:11:11,886 --> 00:11:12,626
actual effect.

258
00:11:12,896 --> 00:11:15,696
It's rather a human convention,
programmer's convention

259
00:11:15,946 --> 00:11:17,946
that we've adopted in
the world of Objective-C,

260
00:11:18,086 --> 00:11:20,816
that even if your init
method doesn't do anything,

261
00:11:20,996 --> 00:11:22,696
it should exist by convention.

262
00:11:22,696 --> 00:11:25,056
You should call it
so this line of code

263
00:11:25,056 --> 00:11:26,946
out of context is not complete.

264
00:11:26,946 --> 00:11:29,366
We have not initialized
the object just yet.

265
00:11:29,756 --> 00:11:32,856
Now because student, and
in the previous slide Foo,

266
00:11:33,066 --> 00:11:35,056
descend from presumably
NSObjects,

267
00:11:35,056 --> 00:11:37,476
the folks at Apple
essentially gave us

268
00:11:37,476 --> 00:11:41,106
for free an implementation of
init for any descendant class.

269
00:11:41,246 --> 00:11:43,006
So we could call init here.

270
00:11:43,006 --> 00:11:45,556
We could call init on a
Foo object and rest assured

271
00:11:45,556 --> 00:11:46,986
that it will actually work.

272
00:11:47,246 --> 00:11:48,716
It just so happens
that the person

273
00:11:48,716 --> 00:11:50,006
who implemented the init method

274
00:11:50,006 --> 00:11:52,816
for NSObject just essentially
returned immediately.

275
00:11:52,816 --> 00:11:55,146
They didn't actually do anything
'cause you won't know what the

276
00:11:55,146 --> 00:11:57,476
descendant class is--
how it's supposed

277
00:11:57,476 --> 00:11:58,786
to be configured anyway.

278
00:11:58,826 --> 00:12:01,156
So in short, whenever
allocating an object,

279
00:12:01,306 --> 00:12:03,576
you should always
allocate it with alloc

280
00:12:03,576 --> 00:12:06,356
or some similar mechanism as
we'll see, convenience methods

281
00:12:06,356 --> 00:12:08,346
as they're called,
and initialize it.

282
00:12:08,786 --> 00:12:11,456
But you sometimes have different
ways of initializing objects.

283
00:12:11,456 --> 00:12:14,336
There are methods beyond
just the one called I-N-I-T.

284
00:12:14,366 --> 00:12:16,116
There're longer names
that we'll sometimes see,

285
00:12:16,436 --> 00:12:17,706
and we can create
them ourselves.

286
00:12:18,906 --> 00:12:19,226
Yes.

287
00:12:19,576 --> 00:12:23,056
>> Autorelease pool will release
that memory at some point?

288
00:12:23,196 --> 00:12:23,896
>> Good question.

289
00:12:24,616 --> 00:12:27,466
It's not so much that the
autorelease pool will release

290
00:12:27,466 --> 00:12:31,896
that memory at some point, but
we have created in main a pool

291
00:12:31,896 --> 00:12:34,406
of memory designed
to be autoreleased

292
00:12:34,406 --> 00:12:36,456
by the operating system later.

293
00:12:37,116 --> 00:12:40,736
So short answer, yes, but
not quite with hat language.

294
00:12:41,976 --> 00:12:44,786
This object will
eventually be freed

295
00:12:44,906 --> 00:12:47,326
when the operating system
realizes no one needs this

296
00:12:47,326 --> 00:12:48,196
object anymore.

297
00:12:48,196 --> 00:12:50,746
And that's the difference
vis-a-vis two years ago, we,

298
00:12:50,746 --> 00:12:52,726
the programmers would
have had to keep track

299
00:12:52,796 --> 00:12:55,146
of when we're done using
this student object

300
00:12:55,316 --> 00:12:58,566
and explicitly free it
or deallocate it later.

301
00:12:58,566 --> 00:13:00,086
But we no longer
have to do that.

302
00:13:01,136 --> 00:13:03,466
All right, just a couple
other things to recap.

303
00:13:03,616 --> 00:13:05,406
So there's that student
init method.

304
00:13:05,626 --> 00:13:07,276
And I keep calling
these methods,

305
00:13:07,276 --> 00:13:10,366
but really in Objective-C the
lexicon is a little different.

306
00:13:10,366 --> 00:13:13,366
When you have open bracket
student init close bracket,

307
00:13:13,726 --> 00:13:17,346
you're not exactly
calling the init method,

308
00:13:17,556 --> 00:13:22,876
you're passing the init
message to the student objects.

309
00:13:22,876 --> 00:13:25,076
Now at the end of the day, it's
functionally the same thing.

310
00:13:25,076 --> 00:13:27,096
It's just sort of a
different view of the world.

311
00:13:27,396 --> 00:13:29,666
But when you see
messages being described,

312
00:13:29,666 --> 00:13:32,406
the message is the act of
calling a method if you come

313
00:13:32,406 --> 00:13:34,796
from the world of Java
or C++ or other languages

314
00:13:34,796 --> 00:13:36,946
that have more conventional
methods.

315
00:13:37,206 --> 00:13:38,436
How about these bottom two?

316
00:13:39,216 --> 00:13:41,666
Student age and student set age.

317
00:13:41,666 --> 00:13:46,156
What do you think the first
one, student age, does?

318
00:13:46,786 --> 00:13:47,866
It's a valid expression.

319
00:13:47,866 --> 00:13:47,933
Yeah.

320
00:13:48,256 --> 00:13:48,946
>> A getter.

321
00:13:49,256 --> 00:13:49,796
>> It's a getter.

322
00:13:50,056 --> 00:13:52,466
So that presumably
returns the value

323
00:13:52,466 --> 00:13:56,256
of an instance variable
called age.

324
00:13:57,156 --> 00:14:00,066
All right, how about
the one below it?

325
00:14:00,796 --> 00:14:04,216
So a setter, so in this case
it's presumably setting some

326
00:14:04,216 --> 00:14:07,446
instance variable
to the value 20.

327
00:14:07,446 --> 00:14:10,536
All right, and it turns
out as you may recall,

328
00:14:10,536 --> 00:14:12,586
the instance variables
underneath the hood might not

329
00:14:12,586 --> 00:14:14,176
even be called that exactly.

330
00:14:14,176 --> 00:14:16,436
That's implementation detail
for which there are conventions.

331
00:14:16,436 --> 00:14:19,156
But we'll see over time how
we can choose those instance

332
00:14:19,156 --> 00:14:20,126
variables on our own.

333
00:14:20,156 --> 00:14:23,386
All right, a couple more complex
topics, not that you have

334
00:14:23,386 --> 00:14:25,596
to be totally comfortable with
just yet but just be mindful

335
00:14:25,596 --> 00:14:26,946
of as we see them later tonight.

336
00:14:27,346 --> 00:14:31,316
So a category allows us
to do what, if you recall?

337
00:14:31,766 --> 00:14:31,833
Yeah.

338
00:14:31,856 --> 00:14:35,596
>> You can-- it's like an
extension to the class.

339
00:14:35,726 --> 00:14:36,926
>> It's like an extension
to a class.

340
00:14:36,926 --> 00:14:37,596
What do you mean by that?

341
00:14:38,006 --> 00:14:48,506
>> So you-- if you had NSString
for example and you wanted

342
00:14:48,506 --> 00:14:51,306
to add more methods to
that or [inaudible] do

343
00:14:51,306 --> 00:14:52,876
that with your category.

344
00:14:52,876 --> 00:14:54,686
>> Yeah, exactly, a
category allows you

345
00:14:54,856 --> 00:14:57,626
to add more methods
to an existing class.

346
00:14:57,626 --> 00:15:01,336
This was-- is particularly
helpful for large classes

347
00:15:01,336 --> 00:15:04,026
that might have let's say
a hundred different methods

348
00:15:04,146 --> 00:15:08,006
or a hundred different selectors
that you can pass to objects,

349
00:15:08,006 --> 00:15:10,546
and you just want to
kind of categorize them.

350
00:15:10,546 --> 00:15:14,616
You want to have the messages
related to this functionality,

351
00:15:14,616 --> 00:15:17,826
different messages related
to this functionality.

352
00:15:17,826 --> 00:15:21,406
You can put them in
distinct categories.

353
00:15:21,406 --> 00:15:25,356
You can put them
in distinct files.

354
00:15:25,356 --> 00:15:27,706
Different people can
work on different subsets

355
00:15:27,706 --> 00:15:29,686
or functionality for a class.

356
00:15:29,686 --> 00:15:31,786
So they're useful in that
regard, somewhat similar

357
00:15:31,786 --> 00:15:33,756
in spirit to JavaScript and the
prototype property whereby you

358
00:15:33,786 --> 00:15:34,956
can add functionality
to existing objects

359
00:15:34,986 --> 00:15:35,826
by a somewhat similar mechanism.

360
00:15:35,856 --> 00:15:37,326
All right, and we will also
see them in the context sort

361
00:15:37,356 --> 00:15:38,436
of a clever trick that
folks have adopted.

362
00:15:38,466 --> 00:15:39,216
We'll also see them as a means

363
00:15:39,246 --> 00:15:40,806
of implementing effectively
private members, private methods

364
00:15:40,836 --> 00:15:42,486
or instance variables to a class
that you don't want the rest

365
00:15:42,516 --> 00:15:44,076
of the world to know about by
putting them in your dot H file.

366
00:15:44,106 --> 00:15:44,886
But we'll see that by example.

367
00:15:44,916 --> 00:15:44,983
Yeah?

368
00:15:45,516 --> 00:15:51,806
[ Inaudible Remark ]

369
00:15:52,306 --> 00:15:54,306
Good question.

370
00:15:54,306 --> 00:15:56,466
So by what guideline
do we decide

371
00:15:56,466 --> 00:15:59,076
where to put our
method declarations,

372
00:15:59,076 --> 00:16:01,706
for instance why might we
put baz inside of a category

373
00:16:01,706 --> 00:16:03,446
as opposed to somewhere else.

374
00:16:03,856 --> 00:16:05,726
So, a couple of reasons,
just to recap.

375
00:16:05,726 --> 00:16:09,456
One might be if I am the guy
implementing the bar category

376
00:16:09,526 --> 00:16:13,326
of methods, I just have chosen
for development convenience

377
00:16:13,326 --> 00:16:16,096
to put them all in one file,
therefore all in one category

378
00:16:16,096 --> 00:16:18,086
so that I can kind of mesh
nicely with colleagues

379
00:16:18,086 --> 00:16:21,066
who are implementing other
categories of methods.

380
00:16:21,306 --> 00:16:22,936
So it's just programmatic
convenience.

381
00:16:22,936 --> 00:16:26,736
But we'll also see
instances where if you want

382
00:16:27,686 --> 00:16:30,446
to have instance methods
associated with a class

383
00:16:31,016 --> 00:16:33,346
but then you want them
effectively to be private,

384
00:16:33,956 --> 00:16:37,156
not broadcasted or advertised
to the world, you don't want

385
00:16:37,156 --> 00:16:39,056
to put them therefore
in your dot H file

386
00:16:39,056 --> 00:16:40,686
because then anyone
poking around it,

387
00:16:40,686 --> 00:16:43,136
your library would
see them by nature

388
00:16:43,136 --> 00:16:44,606
of what a header
file is meant to be.

389
00:16:44,606 --> 00:16:46,086
It's meant to be
included by other people

390
00:16:46,086 --> 00:16:47,426
or imported by other people.

391
00:16:47,826 --> 00:16:50,796
So you can tuck them away
and still document them

392
00:16:50,796 --> 00:16:53,506
and still teach the compiler
that they exist which is helpful

393
00:16:53,506 --> 00:16:56,246
for you using Xcode since it
knows more about your code,

394
00:16:56,716 --> 00:16:59,446
but you're not advertising
them to the rest of the world.

395
00:16:59,446 --> 00:17:01,056
And it doesn't make
them truly private

396
00:17:01,106 --> 00:17:03,756
because you can still send
any message to any object

397
00:17:03,876 --> 00:17:07,166
that you want and just on a
whim hoping that it exists.

398
00:17:07,166 --> 00:17:09,096
And if it exists it
will be responded to.

399
00:17:09,476 --> 00:17:10,346
But it's a mechanism

400
00:17:10,346 --> 00:17:12,906
to approximate that
idea of privacy.

401
00:17:12,906 --> 00:17:15,086
It's just not strictly enforced.

402
00:17:15,806 --> 00:17:18,616
So I would think I'd keep
those two guidelines in mind.

403
00:17:19,086 --> 00:17:20,456
And lastly, protocols.

404
00:17:20,866 --> 00:17:24,246
This is more complex
manifestation of this

405
00:17:24,246 --> 00:17:25,746
than we'll see later today

406
00:17:25,976 --> 00:17:39,196
but a protocol let's us
do what in Objective-C.

407
00:17:39,196 --> 00:17:39,646
Yeah?

408
00:17:39,646 --> 00:17:41,896
>> [Inaudible] set of methods
that should be implemented

409
00:17:41,896 --> 00:17:45,386
by the class that
implements the protocol.

410
00:17:45,716 --> 00:17:49,016
>> Exactly, it allows us
to define a set of methods

411
00:17:49,016 --> 00:17:51,156
that should be implemented
by some class

412
00:17:51,156 --> 00:17:53,336
that adheres to that protocol.

413
00:17:53,336 --> 00:17:56,686
So if you're familiar with Java,
it's effectively an interface.

414
00:17:56,686 --> 00:17:59,586
And other languages have
the same conventions.

415
00:17:59,706 --> 00:18:03,706
It is a set of methods that you,
a class, promised to implement

416
00:18:03,706 --> 00:18:09,316
if you declare yourself as
adhering to that protocol.

417
00:18:09,426 --> 00:18:14,136
And there are ways of specifying
that the methods are optional.

418
00:18:14,626 --> 00:18:19,966
But by default, they would
typically be required.

419
00:18:19,966 --> 00:18:22,636
And the compiler
will yell at you

420
00:18:22,636 --> 00:18:27,766
if you say I am implementing
the NSCopying protocol, but you,

421
00:18:27,856 --> 00:18:30,836
the programmer, forget
to actually get

422
00:18:30,836 --> 00:18:34,176
around to implementing
the required methods

423
00:18:38,416 --> 00:18:39,816
to make that possible.

424
00:18:39,846 --> 00:18:42,466
All right, so the
biggest takeaways

425
00:18:42,466 --> 00:18:45,156
from this recap I would say
are one, what a pointer is.

426
00:18:45,156 --> 00:18:46,756
Two, what an object is.

427
00:18:46,756 --> 00:18:50,836
And I think those
two would make--

428
00:18:50,836 --> 00:18:55,706
make me pretty happy
moving forward.

429
00:18:55,976 --> 00:18:58,836
So just a recap, an
object is an instantiation

430
00:18:59,036 --> 00:19:01,296
or an instance of a class.

431
00:19:01,296 --> 00:19:02,606
A pointer is?

432
00:19:02,756 --> 00:19:03,616
>> That's an address.

433
00:19:03,616 --> 00:19:05,756
>> It's an address of some
piece of data and then

434
00:19:05,756 --> 00:19:08,536
that will often be the
address of some objects.

435
00:19:08,536 --> 00:19:12,366
So if you're comfortable
with those two definitions,

436
00:19:12,366 --> 00:19:14,076
I think we can get through this.

437
00:19:14,076 --> 00:19:14,456
All right, so iOS.

438
00:19:14,576 --> 00:19:19,136
So, iOS adheres to a design
paradigm generally known as MVC

439
00:19:19,136 --> 00:19:23,316
which stands for, as you might
guess, Model View Controller.

440
00:19:23,316 --> 00:19:28,286
So we're not really going to
spend any time on the M today.

441
00:19:28,356 --> 00:19:30,806
But we will spend time
on the C and the V,

442
00:19:30,806 --> 00:19:32,056
the controller and the view.

443
00:19:32,056 --> 00:19:33,896
And you can think of the
controller of a program

444
00:19:33,896 --> 00:19:36,356
that adheres to this
MVC paradigm

445
00:19:36,356 --> 00:19:39,376
as really being the
brains of your program.

446
00:19:39,376 --> 00:19:42,276
It's where you probably
spend much

447
00:19:42,476 --> 00:19:44,906
of your time implementing
your program's logic,

448
00:19:44,946 --> 00:19:47,766
responding to user interactions,
displaying things on the screen.

449
00:19:47,796 --> 00:19:49,176
But with that last goal,
displaying things on the screen,

450
00:19:49,206 --> 00:19:50,766
all you do is tell someone else
to actually do the rendering

451
00:19:50,796 --> 00:19:51,516
of information on the screen.

452
00:19:51,546 --> 00:19:52,596
So if I'm a program
for instance that's--

453
00:19:52,626 --> 00:19:53,196
whose purpose in life is

454
00:19:53,226 --> 00:19:54,666
to display someone's account
balance in their bank account,

455
00:19:54,696 --> 00:19:55,596
I being the controller,
would respond

456
00:19:55,626 --> 00:19:56,526
to the user's username
and password.

457
00:19:56,556 --> 00:19:57,486
I might somehow check
that their username

458
00:19:57,516 --> 00:19:58,416
and password are
correct when they log

459
00:19:58,446 --> 00:19:59,046
into the bank's website.

460
00:19:59,076 --> 00:20:00,216
And then I, the controller,
upon realizing, OK,

461
00:20:00,246 --> 00:20:01,746
this is user David, I'm going
to go check his bank account

462
00:20:01,776 --> 00:20:03,336
and I'm going to check that by
asking someone called the model,

463
00:20:03,366 --> 00:20:04,146
but more on that in the future.

464
00:20:04,176 --> 00:20:06,036
Once I have that value like 100
dollars, I'm then going to hand

465
00:20:06,066 --> 00:20:07,626
that value 100.00 to
something called a view

466
00:20:07,656 --> 00:20:08,976
to actually present that
information to the user.

467
00:20:09,046 --> 00:20:11,516
So in other words, data is
typically stored in the model.

468
00:20:11,516 --> 00:20:13,486
In this case, that's where
my financial numbers might

469
00:20:13,486 --> 00:20:14,016
be stored.

470
00:20:14,176 --> 00:20:15,566
The controller is
going to be the brains.

471
00:20:15,566 --> 00:20:17,486
All of the code that
you write that sort of--

472
00:20:17,676 --> 00:20:20,496
it respond to ifs and
elses and for loops

473
00:20:20,496 --> 00:20:23,066
and while loops will often
be inside of your controller.

474
00:20:23,266 --> 00:20:26,116
And then when it comes time
to display the results of all

475
00:20:26,116 --> 00:20:27,866
that number crunching
to the user,

476
00:20:28,016 --> 00:20:30,716
you typically hand the
data that you have gathered

477
00:20:30,716 --> 00:20:33,066
or computed off to
something called the view.

478
00:20:33,266 --> 00:20:35,156
And the view takes
at that last mile.

479
00:20:35,156 --> 00:20:39,346
It's sort of the HTML and the
CSS of the world of the web

480
00:20:39,346 --> 00:20:40,576
in the context of iOS.

481
00:20:40,686 --> 00:20:41,536
It's going to have to do

482
00:20:41,536 --> 00:20:44,116
with what the user sees
on the glass screen.

483
00:20:44,396 --> 00:20:46,946
So this is a paradigm that
you should keep in mind

484
00:20:47,226 --> 00:20:49,186
because that many
junctures, particularly

485
00:20:49,186 --> 00:20:51,776
with the iOS project,
the staff's choice,

486
00:20:52,066 --> 00:20:53,456
you'll have to decide
for yourself

487
00:20:53,456 --> 00:20:55,016
where do I put this variable.

488
00:20:55,236 --> 00:20:56,846
Where do I store this data?

489
00:20:56,846 --> 00:20:59,286
Where do I actually display
something on the screen,

490
00:20:59,486 --> 00:21:03,136
and the goal with adhering to
a design paradigm like MVC is

491
00:21:03,326 --> 00:21:07,246
to not blur these lines and to
adhere to these best practices

492
00:21:07,486 --> 00:21:09,516
so that when you start
collaborating with someone else

493
00:21:09,696 --> 00:21:12,736
or when some new device comes
out like the newest iPad

494
00:21:12,736 --> 00:21:14,846
which is maybe bigger than
the two existing iPads,

495
00:21:15,166 --> 00:21:17,416
you don't have to change how
you're representing your data.

496
00:21:17,656 --> 00:21:20,016
You don't have to change any of
your so-called business logic,

497
00:21:20,086 --> 00:21:21,116
the guts of your program.

498
00:21:21,336 --> 00:21:24,836
All you have to do is
change or add a new view.

499
00:21:24,836 --> 00:21:28,126
And this makes it super easy to
deal with things like iPhones

500
00:21:28,126 --> 00:21:30,786
and Windows Phones
and Android devices.

501
00:21:30,786 --> 00:21:33,286
And all of the different
displays or views

502
00:21:33,576 --> 00:21:36,106
that a user might interact
with, you don't have

503
00:21:36,106 --> 00:21:37,546
to touch the essence
of your program.

504
00:21:37,546 --> 00:21:38,796
And that just makes life much,

505
00:21:38,796 --> 00:21:40,926
much easier as your
programs get more complex

506
00:21:40,926 --> 00:21:42,406
and more interesting.

507
00:21:42,856 --> 00:21:48,766
All right, so here is now,
oops, how an iPhone application

508
00:21:48,766 --> 00:21:51,496
or an iPad application
works in more detail

509
00:21:51,496 --> 00:21:52,586
than we need for today.

510
00:21:52,766 --> 00:21:55,276
But let me go ahead and zoom
in on a portion of this.

511
00:21:55,636 --> 00:21:58,386
The key feature of this picture
is the fact that there is

512
00:21:58,386 --> 00:22:00,996
that circle in the bottom left
hand corner generally known

513
00:22:00,996 --> 00:22:02,006
as an event loop.

514
00:22:02,246 --> 00:22:04,256
And as you might imagine,
if you have an iPhone

515
00:22:04,256 --> 00:22:07,046
or an Android device or anything
that's a touchscreen these days,

516
00:22:07,046 --> 00:22:09,486
for the most part it's just
sitting there for the entirety

517
00:22:09,486 --> 00:22:12,396
of its life waiting for you
to do something or waiting

518
00:22:12,396 --> 00:22:15,786
for something external to happen
like an SMS message coming in

519
00:22:15,976 --> 00:22:17,396
or a phone call coming in.

520
00:22:17,606 --> 00:22:19,716
So it makes sense that the
essence of a smartphone

521
00:22:19,716 --> 00:22:21,956
or a tablet these
days is just to sit

522
00:22:21,956 --> 00:22:24,446
in an infinite loop waiting
for something to happen.

523
00:22:24,526 --> 00:22:26,456
And that's indeed
what's really happening

524
00:22:26,456 --> 00:22:27,846
in some form underneath
the hood.

525
00:22:28,086 --> 00:22:30,506
So this is a picture from
Apple's documentation.

526
00:22:30,506 --> 00:22:32,366
The URL for which is at the
bottom of the slide if you'd

527
00:22:32,366 --> 00:22:33,506
like to read the whole article.

528
00:22:33,756 --> 00:22:36,156
And when you launch an
application, you tap an icon.

529
00:22:36,526 --> 00:22:39,116
The main function gets invoked
which just like in the world

530
00:22:39,116 --> 00:22:41,456
of C and Objective-C independent

531
00:22:41,456 --> 00:22:43,306
of iOS is the first
function that gets called.

532
00:22:43,676 --> 00:22:45,496
We'll see that there's
typically a function called

533
00:22:45,496 --> 00:22:50,086
UIApplicationMain that's called
by default, by convention just

534
00:22:50,086 --> 00:22:52,616
because Apple designed the
infrastructure to run that way.

535
00:22:52,906 --> 00:22:55,436
And then we load
the main UI file.

536
00:22:55,436 --> 00:22:59,996
UI being just user interface,
so something having to do

537
00:22:59,996 --> 00:23:01,796
with the visual aspects
of my program.

538
00:23:02,076 --> 00:23:04,136
First initialization,
whatever that means.

539
00:23:04,136 --> 00:23:07,396
Maybe it's loading some of
my top scores if it's a game

540
00:23:07,396 --> 00:23:09,726
or maybe it's loading my bank
account information from cash,

541
00:23:09,726 --> 00:23:11,136
something like that,
some piece of data.

542
00:23:11,636 --> 00:23:13,416
And now we have a
fork in the road.

543
00:23:13,416 --> 00:23:16,126
Well let's see-- let's just
go straight down and assume

544
00:23:16,126 --> 00:23:17,976
that I haven't written
any code yet.

545
00:23:18,226 --> 00:23:20,556
We're going to restore
the UI state.

546
00:23:20,556 --> 00:23:23,846
So if I for instance played
this game five minutes ago

547
00:23:23,846 --> 00:23:26,466
but then had to take a call,
maybe I need to redisplay

548
00:23:26,466 --> 00:23:28,746
to the user where they
paused the game rather

549
00:23:28,746 --> 00:23:29,766
than starting from scratch.

550
00:23:29,766 --> 00:23:31,736
So that's what we mean
by restore UI state.

551
00:23:31,906 --> 00:23:35,156
Final initialization,
whatever that means

552
00:23:35,306 --> 00:23:36,406
for my particular program.

553
00:23:36,406 --> 00:23:39,636
And then once we reached
this blue rectangle,

554
00:23:40,026 --> 00:23:43,006
we've activated the app and just
start listening for the user

555
00:23:43,006 --> 00:23:45,526
to touch the screen,
for a monster to come

556
00:23:45,526 --> 00:23:47,756
by on the screen,
for a phone call

557
00:23:47,756 --> 00:23:49,576
to come in, for an
SMS to arrive.

558
00:23:49,836 --> 00:23:52,426
We just keep waiting for
something interesting to happen.

559
00:23:52,726 --> 00:23:54,366
Meanwhile, stuff can happen.

560
00:23:54,626 --> 00:23:56,686
And this line going from
left to right to left

561
00:23:56,686 --> 00:24:00,236
to right is we're pointing at
this box that says handle events

562
00:24:00,296 --> 00:24:02,446
where an event is any of
the things I just described,

563
00:24:02,446 --> 00:24:05,676
something external triggered by
the user or external triggered

564
00:24:05,676 --> 00:24:07,506
by the network to
which you're connected.

565
00:24:07,796 --> 00:24:11,176
Now meanwhile, we have some
slightly more technical detail

566
00:24:11,176 --> 00:24:11,596
on the right.

567
00:24:11,946 --> 00:24:13,716
This is the last
portion of this picture.

568
00:24:13,926 --> 00:24:17,056
On the right, everything in
this big white box is my code

569
00:24:17,056 --> 00:24:17,816
that I've written.

570
00:24:17,816 --> 00:24:21,126
So we've seen a monospaced
font some functions or methods

571
00:24:21,176 --> 00:24:23,326
that we'll soon see
in some actual code.

572
00:24:23,616 --> 00:24:25,796
And for instance,
one of the functions

573
00:24:25,796 --> 00:24:29,136
up there is called application
willFinishLaunchingWithOptions.

574
00:24:29,136 --> 00:24:32,516
This is a common characteristic
of Objective-C methods.

575
00:24:32,516 --> 00:24:35,516
They are atrociously long named
but they're very descriptive

576
00:24:35,516 --> 00:24:39,626
and they capture-- their names
describe what their arguments

577
00:24:39,696 --> 00:24:42,376
or parameters are,
various methods

578
00:24:42,376 --> 00:24:43,706
which is a nice hand wave there.

579
00:24:43,886 --> 00:24:46,456
Application
didFinishLaunchingWithOptions.

580
00:24:46,456 --> 00:24:48,036
So notice the difference.

581
00:24:48,366 --> 00:24:51,256
First one will finish so it's
about to finish launching.

582
00:24:51,526 --> 00:24:53,296
Did finish means
it's after the fact.

583
00:24:53,296 --> 00:24:56,306
So if your code needs to do
something before or after

584
00:24:56,306 --> 00:24:58,056
that change in state,
you have that control.

585
00:24:58,336 --> 00:25:02,176
And then lastly down here,
applicationDidBecomeActive.

586
00:25:02,446 --> 00:25:04,226
So if you have-- [inaudible]
implemented a game,

587
00:25:04,526 --> 00:25:06,996
this kind of implies
that you can

588
00:25:06,996 --> 00:25:09,666
for instance immediately
resume the game because somehow

589
00:25:09,666 --> 00:25:11,866
or other Apple is providing
you with these hooks

590
00:25:11,866 --> 00:25:13,256
into interesting key events.

591
00:25:13,256 --> 00:25:16,036
And this is quite like we saw
in JavaScript with the world

592
00:25:16,036 --> 00:25:17,856
of DOM, onclick, onsubmit.

593
00:25:17,856 --> 00:25:19,396
All of the interesting
things that can happen

594
00:25:19,396 --> 00:25:20,826
in JavaScript can happen

595
00:25:20,826 --> 00:25:23,326
in similar spirit
in the world of iOS.

596
00:25:23,836 --> 00:25:26,896
So this white box is what we're
going to start writing today.

597
00:25:27,176 --> 00:25:29,766
And Apple has written
all of this stuff for us

598
00:25:30,046 --> 00:25:32,096
and essentially just
allows us to hook

599
00:25:32,096 --> 00:25:35,626
into this event loop waiting and
listening for interesting things

600
00:25:35,626 --> 00:25:39,336
to happen just like
JavaScript and jQuery allow us

601
00:25:39,336 --> 00:25:42,076
to do quite readily
in the world of HTML.

602
00:25:42,356 --> 00:25:42,466
Yeah?

603
00:25:43,516 --> 00:25:51,836
[ Inaudible Remark ]

604
00:25:52,336 --> 00:25:55,006
Good question.

605
00:25:55,386 --> 00:25:57,586
Short answer is it
depends on who,

606
00:25:57,716 --> 00:26:00,526
what kind of action is
and where it happens.

607
00:26:00,566 --> 00:26:03,946
For instance, if you have
a program that's running

608
00:26:04,306 --> 00:26:05,856
and you get a phone call,

609
00:26:06,076 --> 00:26:08,656
Apple's threads can
take higher priority

610
00:26:08,656 --> 00:26:11,436
and therefore interrupt your
program and actually show you

611
00:26:11,436 --> 00:26:13,156
on the screen who
is actually calling.

612
00:26:13,546 --> 00:26:17,516
So multithreaded, yes, but
not necessarily in the sense

613
00:26:17,576 --> 00:26:20,206
that you can't continue
running while

614
00:26:20,206 --> 00:26:21,746
that phone call is in progress.

615
00:26:21,746 --> 00:26:24,376
So there's certain design
paradigms that Apple adheres

616
00:26:24,376 --> 00:26:26,886
to that forces your
threads to go to sleep

617
00:26:27,246 --> 00:26:28,926
when more important
things happen.

618
00:26:28,926 --> 00:26:31,256
And this is in contrast to
the world of say Android

619
00:26:31,256 --> 00:26:32,556
which lets you do a lot more.

620
00:26:32,886 --> 00:26:35,826
And the argument
longstanding for Apple has been

621
00:26:35,826 --> 00:26:38,076
that this way they can
police battery life better

622
00:26:38,076 --> 00:26:39,226
so that you don't
have some thread

623
00:26:39,226 --> 00:26:41,076
in the background just
churning and churning away.

624
00:26:41,446 --> 00:26:43,426
But the price we pay
is that if you run

625
00:26:43,426 --> 00:26:45,156
for instance the Gmail
app on your phone,

626
00:26:45,406 --> 00:26:48,436
you can get notifications and be
informed that you have new mail

627
00:26:48,436 --> 00:26:50,656
with the little badge
number being incremented.

628
00:26:50,826 --> 00:26:52,356
But if you want to
actually check your mail,

629
00:26:52,396 --> 00:26:56,116
you have to load Gmail, refresh
the screen, wait a few seconds

630
00:26:56,116 --> 00:26:57,366
and then see your email.

631
00:26:57,476 --> 00:26:59,516
And I bring this up as an
example 'cause it drives

632
00:26:59,516 --> 00:27:00,456
me nuts.

633
00:27:00,456 --> 00:27:04,346
Whereas the built-in mail
application has more-- a more--

634
00:27:04,346 --> 00:27:08,346
is more tightly coupled
with the operating system

635
00:27:08,346 --> 00:27:09,696
and can actually do all of that

636
00:27:09,696 --> 00:27:12,226
in the background
in its own thread.

637
00:27:13,256 --> 00:27:17,246
All right, so UIKit is where
we begin our look today.

638
00:27:17,246 --> 00:27:20,826
And UIKit is just the
user interface related SDK

639
00:27:20,826 --> 00:27:23,636
that Apple provides to the
world, software development kit.

640
00:27:23,966 --> 00:27:26,226
So these are some class names

641
00:27:26,226 --> 00:27:28,156
that will quickly
become familiar to us.

642
00:27:28,386 --> 00:27:31,226
Some more so than others and
they describe different classes

643
00:27:31,226 --> 00:27:33,266
that allow us to do
different things.

644
00:27:33,266 --> 00:27:36,606
UIApplication is the class that
describes an iOS application.

645
00:27:36,976 --> 00:27:39,896
UIApplicationDelegate is similar
in spirit and we'll come back

646
00:27:39,996 --> 00:27:42,926
to delegation and
protocols later today.

647
00:27:43,226 --> 00:27:46,536
UIView describes the
aesthetics of a program.

648
00:27:46,536 --> 00:27:49,016
It's a class that represents
something that's on the screen,

649
00:27:49,016 --> 00:27:50,826
maybe it's a rectangle,
maybe it's a button,

650
00:27:50,826 --> 00:27:51,796
maybe it's a text field.

651
00:27:51,796 --> 00:27:52,736
So something like that.

652
00:27:53,236 --> 00:27:56,226
UIViewController is
unfortunately named--

653
00:27:56,226 --> 00:27:59,406
Apple calls controllers
view controllers.

654
00:27:59,546 --> 00:28:00,986
So they are distinct.

655
00:28:00,986 --> 00:28:05,216
View controller is the C. UIView
is the V. View controller is

656
00:28:05,216 --> 00:28:08,176
again just a C, not
a V. It's the brains

657
00:28:08,176 --> 00:28:09,476
of our application typically.

658
00:28:09,646 --> 00:28:11,066
And UIWindow is just a class

659
00:28:11,066 --> 00:28:13,946
that represents the rectangular
piece of glass in some form

660
00:28:14,286 --> 00:28:17,026
that your application
is going to run inside.

661
00:28:17,506 --> 00:28:20,406
So let's go ahead and write
our first application.

662
00:28:20,406 --> 00:28:22,356
It turns out we have
different starting points

663
00:28:22,386 --> 00:28:23,766
that Xcode provides us with.

664
00:28:24,076 --> 00:28:26,946
We don't really have to
use any template code

665
00:28:26,946 --> 00:28:28,836
that Apple provides, but
you'll typically find

666
00:28:28,836 --> 00:28:31,296
that it's a great way to
get started quickly even

667
00:28:31,296 --> 00:28:33,356
if you just start with
an empty application.

668
00:28:33,356 --> 00:28:34,006
So let's do this.

669
00:28:34,406 --> 00:28:40,456
Let's see if we start by
loading X code here and going

670
00:28:40,456 --> 00:28:42,886
up to the file menu and
going to new project.

671
00:28:42,886 --> 00:28:45,016
So not new file but new project.

672
00:28:45,276 --> 00:28:46,626
So I get a whole
bunch of folders

673
00:28:46,626 --> 00:28:48,066
and a whole bunch
of files for free.

674
00:28:48,356 --> 00:28:51,086
Notice that we've got
some organization here.

675
00:28:51,086 --> 00:28:54,186
And last week you may recall
poking around with OS X

676
00:28:54,186 --> 00:28:56,466
because we just needed some
simple Objective-C programs,

677
00:28:56,466 --> 00:28:57,516
not necessarily iOS.

678
00:28:57,886 --> 00:29:00,456
Tonight and hereafter we
focus only on the top half

679
00:29:00,546 --> 00:29:03,996
of that menu under iOS where we
have an application category,

680
00:29:03,996 --> 00:29:05,416
framework and library and other.

681
00:29:05,656 --> 00:29:07,316
Most everything we'll
be interested in.

682
00:29:07,316 --> 00:29:10,126
For now we'll be
under iOS application.

683
00:29:10,336 --> 00:29:13,596
So here are those same template
names we saw a moment ago.

684
00:29:13,806 --> 00:29:15,956
And if you're familiar
with an iOS device,

685
00:29:15,956 --> 00:29:18,326
you probably recognize some
of the design paradigms here.

686
00:29:18,326 --> 00:29:21,106
The master detail view looks
like it has a little menu

687
00:29:21,106 --> 00:29:23,126
on the left hand side
and then more information

688
00:29:23,126 --> 00:29:25,646
on the right hand side like
the built-in mail application.

689
00:29:25,916 --> 00:29:28,346
OpenGL Game refers to
a graphics-based game.

690
00:29:28,596 --> 00:29:30,726
Page-based application
means something that has

691
00:29:30,726 --> 00:29:34,356
that sexy page curl that
happens from the bottom up.

692
00:29:34,416 --> 00:29:38,046
Single view application is a
place we'll spend some time

693
00:29:38,046 --> 00:29:41,806
today since it just allows us
to have some basic sample code

694
00:29:42,046 --> 00:29:44,896
and to get started quickly with
a user interface of our own.

695
00:29:45,166 --> 00:29:48,546
Utility application which is
something like the weather

696
00:29:48,726 --> 00:29:50,916
or the stock application
super simple

697
00:29:50,916 --> 00:29:52,836
but typically have a button
that allows you to flip it

698
00:29:52,836 --> 00:29:54,516
around to see some settings.

699
00:29:54,516 --> 00:29:56,526
And indeed that's the
template you'll use

700
00:29:56,526 --> 00:29:58,616
for the next projects
starting Wednesday onward.

701
00:29:58,876 --> 00:30:01,956
And then the last one down
there is tabbed application

702
00:30:02,886 --> 00:30:05,976
which is also a common paradigm
at least in iOS 6 and prior.

703
00:30:06,046 --> 00:30:07,946
We have got some big
icons at the bottom

704
00:30:08,276 --> 00:30:11,186
that toggle state among
different portions

705
00:30:11,186 --> 00:30:12,966
of a program's functionality.

706
00:30:13,296 --> 00:30:17,676
So, let's do something super
simple like Empty Application.

707
00:30:18,166 --> 00:30:19,686
And according to
the instructions,

708
00:30:19,726 --> 00:30:21,086
this template provides
a starting point

709
00:30:21,086 --> 00:30:24,176
for any application, it provides
just an application delegate

710
00:30:24,176 --> 00:30:24,846
and a window.

711
00:30:24,846 --> 00:30:26,466
So, let's see what
this actually gets us.

712
00:30:26,466 --> 00:30:29,296
And what we'll do is today,
spend some time in particular

713
00:30:29,586 --> 00:30:32,546
on what the sample code
is that Apple provides not

714
00:30:32,546 --> 00:30:34,656
because it's interesting but
because it's representative

715
00:30:34,716 --> 00:30:36,906
of how you might write
your own applications.

716
00:30:37,246 --> 00:30:39,936
So, here we have a--
knowing a number of prompts,

717
00:30:39,936 --> 00:30:42,486
I'm going to go ahead
and just call this test.

718
00:30:42,486 --> 00:30:45,106
My organization name can
be whatever I want here.

719
00:30:45,106 --> 00:30:47,016
I'll say Harvard University.

720
00:30:47,366 --> 00:30:50,886
Company identifier, you just
need a probabilistically unique

721
00:30:50,886 --> 00:30:53,406
string really so that
if you push an app

722
00:30:53,646 --> 00:30:55,176
to the app store it's distinct.

723
00:30:55,466 --> 00:30:58,636
We'll go with edu.harvard
which is a common paradigm

724
00:30:58,636 --> 00:31:00,306
in the Java world
for packaged names.

725
00:31:00,646 --> 00:31:03,206
If we own harvard.edu,
presumably no one else

726
00:31:03,206 --> 00:31:05,066
in the world is going
to use that same prefix

727
00:31:05,066 --> 00:31:07,306
for their applications but
it's just a human convention.

728
00:31:07,626 --> 00:31:10,366
Class prefix, I'm going to leave
blank 'cause it doesn't matter.

729
00:31:10,366 --> 00:31:12,416
Devices, for lecture I'm going

730
00:31:12,416 --> 00:31:14,076
to typically choose
iPhone really just

731
00:31:14,076 --> 00:31:15,746
so that it fits nicely
in the screen,

732
00:31:16,016 --> 00:31:17,066
so we can see everything.

733
00:31:17,396 --> 00:31:18,906
And then there's a few options.

734
00:31:18,906 --> 00:31:21,646
Here core data we'll come back
to in the future but it has

735
00:31:21,646 --> 00:31:25,166
to do with an abstraction
layer for storing data

736
00:31:25,316 --> 00:31:26,756
in the equivalence
of a database.

737
00:31:27,236 --> 00:31:28,836
Use automatic reference
counting,

738
00:31:28,836 --> 00:31:30,446
is that memory management
feature,

739
00:31:30,446 --> 00:31:32,716
I alluded to earlier
having coming out a couple

740
00:31:32,716 --> 00:31:34,606
of years ago, it makes
our lives easier.

741
00:31:34,606 --> 00:31:35,676
So, we'll leave that on.

742
00:31:35,676 --> 00:31:38,976
And then, include unit test,
so I'll uncheck those for now

743
00:31:38,976 --> 00:31:41,336
but we'll come back to
that in time as to how

744
00:31:41,336 --> 00:31:43,696
to right tests for
your own code.

745
00:31:43,696 --> 00:31:45,556
I'm going to go ahead
and click next now.

746
00:31:46,056 --> 00:31:47,566
It's asking me where to save it.

747
00:31:47,566 --> 00:31:49,756
I'm just going to save it
wherever, on my desktop for now.

748
00:31:50,116 --> 00:31:52,826
For source control, just to
keep my directory simple,

749
00:31:52,826 --> 00:31:53,986
I'm going to uncheck
that for now,

750
00:31:53,986 --> 00:31:57,176
by all means you use the
built-in GIT functionality

751
00:31:57,176 --> 00:31:59,736
for doing version control
and we'll defer to any number

752
00:31:59,736 --> 00:32:02,676
of online tutorials for how to
use that in Xcode itself, yeah.

753
00:32:03,516 --> 00:32:08,676
[ Inaudible Remark ]

754
00:32:09,176 --> 00:32:10,026
>> Good question.

755
00:32:10,026 --> 00:32:11,446
You have to adopt
your code a lot.

756
00:32:11,446 --> 00:32:15,456
If you're developing for iPhones
and iPads, it really depends

757
00:32:15,586 --> 00:32:18,426
on your application and to
what extent you want the user

758
00:32:18,426 --> 00:32:22,576
interface to just grow to fit
or to rearrange itself somewhat.

759
00:32:22,826 --> 00:32:26,516
For instance, if you don't
implement a different view

760
00:32:26,516 --> 00:32:29,306
for the iPad specifically,
what Apple does

761
00:32:29,306 --> 00:32:32,066
by default is it shows
you your iPhone view.

762
00:32:32,336 --> 00:32:35,096
But, in a tiny subset
of the window

763
00:32:35,096 --> 00:32:37,576
and they do give you
a 2X button to zoom in

764
00:32:37,846 --> 00:32:39,756
but most every application
I've ever seen

765
00:32:39,756 --> 00:32:41,236
that just relies on
that looks awful.

766
00:32:41,646 --> 00:32:43,896
So, you would typically
want as a developer

767
00:32:43,896 --> 00:32:46,526
to somehow customize your
interface even if it's just

768
00:32:46,526 --> 00:32:49,226
by providing higher resolution
graphics for the bigger device

769
00:32:49,626 --> 00:32:51,286
so that it doesn't look
like someone just zoomed

770
00:32:51,286 --> 00:32:52,696
in on your application.

771
00:32:52,696 --> 00:32:53,906
But short answer, it depends,

772
00:32:53,906 --> 00:32:57,876
and because of MVC it's
actually relatively easy

773
00:32:58,096 --> 00:33:00,006
by not changing the
essence of your program,

774
00:33:00,006 --> 00:33:03,426
just designing a new view that
has similarly named variables,

775
00:33:03,586 --> 00:33:04,866
so to speak, to hook
into your data.

776
00:33:05,826 --> 00:33:08,016
All right, so now, I'm
going to click Create

777
00:33:08,186 --> 00:33:10,006
and let's start poking
around Xcode.

778
00:33:10,006 --> 00:33:12,026
And let me go ahead and
hide some distractions

779
00:33:12,026 --> 00:33:15,426
for just a moment and here
we have, let me move this

780
00:33:15,996 --> 00:33:18,006
so that everything is in
view on the screen here.

781
00:33:18,396 --> 00:33:20,526
Here's everything we
have to begin with.

782
00:33:20,526 --> 00:33:23,036
I'm going to wave my hand at
all of these stuff in the middle

783
00:33:23,036 --> 00:33:26,466
because generally you can
dive right in without having

784
00:33:26,466 --> 00:33:28,436
to change any of those
configuration details.

785
00:33:28,436 --> 00:33:31,106
And this is the stuff
that's nice to get for free

786
00:33:31,106 --> 00:33:33,326
out of the box rather
than opening up something

787
00:33:33,326 --> 00:33:36,566
like TextEdit and just starting
to write Objective-C code or Vim

788
00:33:36,566 --> 00:33:38,576
or Emacs or whatever your
preferred text editor is.

789
00:33:38,846 --> 00:33:41,456
Xcode just sets up a whole
bunch of defaults for you.

790
00:33:41,746 --> 00:33:43,936
So, let's instead focus
our attention on the files

791
00:33:44,136 --> 00:33:47,196
that you get for free
with an application,

792
00:33:47,226 --> 00:33:48,406
but before we do that,

793
00:33:48,606 --> 00:33:50,916
let me zoom in on the top
left hand side of the screen.

794
00:33:51,136 --> 00:33:54,246
Notice that Xcode is so user
friendly, it's just like iTunes

795
00:33:54,246 --> 00:33:56,296
to run my program, all
I do is click Play.

796
00:33:56,296 --> 00:33:58,766
And if I go and do
that, build succeeded.

797
00:33:59,206 --> 00:33:59,786
What's happening

798
00:33:59,786 --> 00:34:02,226
in the background is my
iPhone application is launched

799
00:34:02,226 --> 00:34:04,336
in the software based
simulator and voila,

800
00:34:04,836 --> 00:34:06,216
I'm now an iPhone programmer.

801
00:34:07,036 --> 00:34:09,186
So, really that's all it
takes to make an iPhone app

802
00:34:09,186 --> 00:34:11,606
and I'm sure back in the day,
I mean, there have been stories

803
00:34:11,606 --> 00:34:15,026
as an aside where you-- someone
made like a ruby app was it

804
00:34:15,026 --> 00:34:19,126
which was just a stupid JPEG of
a ruby, the gem, submitted it

805
00:34:19,126 --> 00:34:23,196
to the app store, put a price
of like-- you maybe recall this,

806
00:34:23,196 --> 00:34:25,916
a thousand dollars and like
four people actually bought it.

807
00:34:25,916 --> 00:34:28,326
And it was really just an
image of a glistening gem.

808
00:34:28,326 --> 00:34:31,146
So, it was that easy for that
person to make that application.

809
00:34:31,236 --> 00:34:32,156
So, this could be you next.

810
00:34:32,536 --> 00:34:35,506
So, what's actually going
on underneath the hood?

811
00:34:35,506 --> 00:34:37,566
Well, let's just notice
we've got a white background,

812
00:34:37,566 --> 00:34:39,776
we still have the
status bar at the top,

813
00:34:39,776 --> 00:34:41,896
so there's still some
built-in functionality there.

814
00:34:42,056 --> 00:34:44,026
And if I minimize it
by going like that,

815
00:34:44,026 --> 00:34:45,596
there indeed is my
test application

816
00:34:45,596 --> 00:34:47,496
on the desktop of my iPhone.

817
00:34:47,496 --> 00:34:49,036
So, let's look at
what files made

818
00:34:49,036 --> 00:34:51,946
that possible uninteresting
though this application is.

819
00:34:52,326 --> 00:34:54,666
So, I'm going to
expand these folders.

820
00:34:55,336 --> 00:34:59,946
As an aside, they're not
necessarily folders in reality.

821
00:35:00,196 --> 00:35:02,566
They look like folders but
they're technically groups,

822
00:35:02,656 --> 00:35:05,386
which means if you poke around
the hard drive of my laptop,

823
00:35:05,566 --> 00:35:07,916
you won't necessarily see
those same yellow folders.

824
00:35:07,916 --> 00:35:11,136
All the files might actually
be in one big folder together.

825
00:35:11,376 --> 00:35:13,796
So, this is just an illusion
for organizing your code

826
00:35:13,796 --> 00:35:16,846
like you might in Eclipse
or something similar.

827
00:35:17,296 --> 00:35:18,406
All right, so it's kind

828
00:35:18,406 --> 00:35:19,926
of overwhelming how
much stuff there is.

829
00:35:20,196 --> 00:35:22,456
So, let's start ignoring
some of these things quickly.

830
00:35:22,716 --> 00:35:25,476
So, at the bottom under
products is Test.app.

831
00:35:26,056 --> 00:35:28,896
This is just a-- I'm the
only one that can see that.

832
00:35:29,276 --> 00:35:32,046
So, at the bottom of the
screen here is Test.app.

833
00:35:32,476 --> 00:35:34,016
That refers to the program

834
00:35:34,436 --> 00:35:37,346
that [inaudible] play
button I already installed

835
00:35:37,466 --> 00:35:38,276
on to my phone.

836
00:35:38,276 --> 00:35:39,866
So, that's the compiled
application.

837
00:35:40,046 --> 00:35:42,116
So, it's not interesting
beyond the fact that it exists.

838
00:35:42,826 --> 00:35:45,186
Now, let's close that to
avoid the distraction.

839
00:35:45,556 --> 00:35:49,206
Under Frameworks, I see UIKit,
Foundation, Core Graphics.

840
00:35:49,656 --> 00:35:51,816
Which of those have we
seen or mentioned before?

841
00:35:53,466 --> 00:35:53,736
>> Foundation.

842
00:35:53,776 --> 00:35:54,656
>> So, Foundation.

843
00:35:54,656 --> 00:35:57,136
So, we saw that last week,
we saw that earlier in some

844
00:35:57,136 --> 00:35:58,246
of our Objective-C code.

845
00:35:58,246 --> 00:36:02,786
That simply had a reference in
the import line to some library

846
00:36:02,786 --> 00:36:04,896
that someone else wrote with a
whole bunch of functionality,

847
00:36:04,896 --> 00:36:06,666
whole bunch of classes and
method that I don't have

848
00:36:06,666 --> 00:36:08,366
to worry about, I
can just use them

849
00:36:08,366 --> 00:36:09,986
without implementing
them myself.

850
00:36:09,986 --> 00:36:11,926
We also heard verbally UIKit.

851
00:36:12,256 --> 00:36:15,016
So, it's inside this library
that we're about to get all

852
00:36:15,016 --> 00:36:18,126
of the interesting functionality
that gives me buttons

853
00:36:18,126 --> 00:36:20,356
and text fields and things
I can touch and click on,

854
00:36:20,576 --> 00:36:21,696
on the glass of a screen.

855
00:36:21,856 --> 00:36:24,296
And Core Graphics we won't
use today but this refers

856
00:36:24,296 --> 00:36:26,066
to a two-dimensional
graphics library

857
00:36:26,226 --> 00:36:28,436
with which we can draw
squares and rectangles

858
00:36:28,436 --> 00:36:30,276
and more interesting
things on the screen.

859
00:36:30,556 --> 00:36:32,456
So, let's collapse
that and ignore.

860
00:36:33,026 --> 00:36:36,256
Now, we have some
pings, so, Default@2X,

861
00:36:36,256 --> 00:36:40,626
Default.png, Default
568h@2X.png.

862
00:36:40,786 --> 00:36:43,536
Does anyone know what these are?

863
00:36:44,256 --> 00:36:45,376
Yeah. Images for?

864
00:36:46,016 --> 00:36:47,806
[ Inaudible Remark ]

865
00:36:47,806 --> 00:36:50,346
Exactly. So, there's
some default images

866
00:36:50,346 --> 00:36:52,596
that if you read the
documentation must be named

867
00:36:52,596 --> 00:36:54,436
exactly this that you
could override them

868
00:36:54,436 --> 00:36:55,346
if you really want it.

869
00:36:55,446 --> 00:36:57,386
And these are just the
images that the user sees

870
00:36:57,386 --> 00:36:59,226
as your application boots off.

871
00:36:59,226 --> 00:37:00,946
Now, for-- or an
application like this,

872
00:37:00,946 --> 00:37:03,186
we barely even saw them because
the thing loaded so fast.

873
00:37:03,596 --> 00:37:06,506
But if you have a network based
application, a game that logs

874
00:37:06,506 --> 00:37:08,766
into some game server, you
might have a couple of seconds

875
00:37:08,766 --> 00:37:10,446
of latency while
the program loads.

876
00:37:10,726 --> 00:37:13,276
This is where you'd see the
Angry Birds splash screen

877
00:37:13,276 --> 00:37:14,576
or something similar like that.

878
00:37:15,006 --> 00:37:17,036
So, it's just a convention,
they're not really interesting.

879
00:37:17,036 --> 00:37:19,196
For our purposes, Apple
gives them to us for free.

880
00:37:19,406 --> 00:37:21,046
So, let's ignore them hereafter.

881
00:37:21,626 --> 00:37:22,866
Test Prefix.pch.

882
00:37:22,866 --> 00:37:26,566
This is a precompiled header
which just means a file

883
00:37:26,566 --> 00:37:29,926
that will do some stuff for
us that will never again,

884
00:37:29,996 --> 00:37:32,416
at least for the next several
weeks have to think about.

885
00:37:32,666 --> 00:37:35,396
In this case it has to do with
some backwards compatibility,

886
00:37:35,566 --> 00:37:40,236
but it's just some additional
metadata that Apple uses

887
00:37:40,236 --> 00:37:43,266
to bootstrap a program
with our--

888
00:37:43,266 --> 00:37:44,556
without our having
to write this.

889
00:37:45,096 --> 00:37:46,396
So, I'm also going
to ignore that.

890
00:37:46,926 --> 00:37:49,136
So, we're really whittling
this down to the essence.

891
00:37:49,136 --> 00:37:51,496
I'm going to skip main.m because
that's clearly of interest.

892
00:37:51,866 --> 00:37:55,326
Test-Info.plist, this is
like a preferences file.

893
00:37:55,326 --> 00:37:57,356
A plist is a property list.

894
00:37:57,596 --> 00:38:00,076
This is just an XML file
underneath the hood,

895
00:38:00,076 --> 00:38:03,256
a whole bunch of tags and
values and attributes and the

896
00:38:03,256 --> 00:38:06,336
like similar to what you
might have seen with Ajax

897
00:38:06,336 --> 00:38:08,286
in the world of DOM, in HTML 5.

898
00:38:08,746 --> 00:38:12,646
Xcode is simply reading that
XML file and showing it to us

899
00:38:12,646 --> 00:38:14,816
in this very pretty fashion,
with rows and columns

900
00:38:14,816 --> 00:38:17,056
but it's just an XML
file underneath the hood.

901
00:38:17,246 --> 00:38:18,586
This is a GUI with
which to edit it.

902
00:38:18,586 --> 00:38:20,866
I'm not going to change
any of these things now

903
00:38:21,116 --> 00:38:23,026
but you can kind of
infer from the names

904
00:38:23,026 --> 00:38:26,966
of the XML elements here what
kinds of things you can tweak,

905
00:38:27,176 --> 00:38:30,746
what interface orientations
you support, up-down, down-up,

906
00:38:30,746 --> 00:38:31,866
left-right, right-left,

907
00:38:32,206 --> 00:38:34,876
those sorts of things can
all be configured by a--

908
00:38:34,876 --> 00:38:37,286
by a point and click by
editing this kind of file.

909
00:38:37,346 --> 00:38:38,866
But we'll stick with
the defaults for now.

910
00:38:39,306 --> 00:38:40,906
If we go back to the
directory structure

911
00:38:40,906 --> 00:38:43,376
over here, infoplist.strings.

912
00:38:43,756 --> 00:38:45,756
So, we'll come back to
this perhaps in the future

913
00:38:45,756 --> 00:38:49,926
but this makes it easy for us
to localize our application.

914
00:38:49,926 --> 00:38:50,696
What does that mean?

915
00:38:50,696 --> 00:38:54,096
>> Translating to
other languages?

916
00:38:54,096 --> 00:38:56,296
>> Yeah, exactly, translating
it to other languages.

917
00:38:56,296 --> 00:38:58,696
So, we won't expect you to
support more than just English

918
00:38:58,696 --> 00:39:01,536
for the staff's choice
of projects but this is

919
00:39:01,536 --> 00:39:05,246
where you could define some key
value pairs where the keys are

920
00:39:05,246 --> 00:39:07,926
like constants that you
use in your actual code

921
00:39:08,156 --> 00:39:10,156
that represent the words
you want your program

922
00:39:10,156 --> 00:39:11,156
to actually display.

923
00:39:11,336 --> 00:39:13,846
And based on whether the user's
device is in Spanish mode

924
00:39:13,846 --> 00:39:15,636
or Chinese mode or English mode,

925
00:39:15,816 --> 00:39:20,416
a different plist file will be
loaded containing those strings

926
00:39:20,696 --> 00:39:23,756
and used on the interface
for your native speaker.

927
00:39:23,756 --> 00:39:26,836
So, it's a nice way of
factoring out all of the words

928
00:39:26,836 --> 00:39:28,596
for a program and putting it--

929
00:39:28,596 --> 00:39:30,696
sorry, not in a plist but
into a dot strings file

930
00:39:30,696 --> 00:39:32,306
which is essentially
just a text file.

931
00:39:32,856 --> 00:39:36,666
So, that leaves only three
files of genuine interest.

932
00:39:36,666 --> 00:39:40,066
So, let's dive into main.m,
and it looks, hopefully,

933
00:39:40,066 --> 00:39:46,136
a little familiar, some new
stuff but mostly boilerplate.

934
00:39:46,136 --> 00:39:46,956
So, let's zoom in.

935
00:39:46,956 --> 00:39:48,706
At the top, we have a
whole bunch of comments

936
00:39:48,756 --> 00:39:51,306
that are outputted
automatically by Xcode depending

937
00:39:51,306 --> 00:39:52,666
on how I've configured
the program

938
00:39:52,666 --> 00:39:53,646
when I first installed it.

939
00:39:53,976 --> 00:39:55,546
First line of interest
is import.

940
00:39:55,676 --> 00:39:57,556
This is apparently giving
me access to what kind

941
00:39:57,556 --> 00:39:59,536
of functionality,
that import line?

942
00:40:01,576 --> 00:40:05,786
Sorry? So, UIKit, so user
interface related stuff.

943
00:40:05,886 --> 00:40:08,326
This is allowing me to write
a graphics-based application

944
00:40:08,326 --> 00:40:10,516
as opposed to the command line
stuff you guys did last week

945
00:40:10,786 --> 00:40:11,356
with Rob.

946
00:40:11,646 --> 00:40:15,106
Import AppDelegate.h, don't
know what that is yet,

947
00:40:15,436 --> 00:40:17,906
but I remember seeing it
among the remaining files,

948
00:40:17,906 --> 00:40:19,236
so we'll come back
to that in a moment.

949
00:40:19,236 --> 00:40:21,406
That's a file that I get
to actually interact with.

950
00:40:21,656 --> 00:40:23,536
Here's the promised
signature for main,

951
00:40:23,536 --> 00:40:24,506
we're not going to touch this.

952
00:40:24,806 --> 00:40:27,086
Here's autorelease pool, and
the only thing that's a little

953
00:40:27,086 --> 00:40:30,706
different now is that apparently
main's sole purpose in life

954
00:40:30,936 --> 00:40:33,726
in an iOS application
is to return,

955
00:40:34,286 --> 00:40:35,846
whatever the return value is,

956
00:40:35,846 --> 00:40:39,816
of a function called
UIApplicationMain passing

957
00:40:39,816 --> 00:40:41,846
in its argument count,
so whatever was typed

958
00:40:41,846 --> 00:40:42,526
at the command line,

959
00:40:42,526 --> 00:40:44,936
which probably has no
significance unless you override

960
00:40:44,936 --> 00:40:46,586
some of the default
settings in Xcode,

961
00:40:46,816 --> 00:40:48,416
passing an argv, same thing.

962
00:40:48,596 --> 00:40:50,826
Nil, whatever that is,
it's essentially null

963
00:40:50,826 --> 00:40:52,156
but with some magic around it.

964
00:40:52,406 --> 00:40:54,246
And then lastly, let
me scroll to the right,

965
00:40:54,906 --> 00:40:58,236
it's a little long, this
string, and it's wrapping,

966
00:40:58,486 --> 00:41:02,856
NSStringFromClass
AppDelegate class.

967
00:41:03,656 --> 00:41:05,826
So let me see if I
can avoid the wrapping

968
00:41:05,826 --> 00:41:07,546
by shrinking the
window a little bit.

969
00:41:08,136 --> 00:41:08,956
So here we go.

970
00:41:09,166 --> 00:41:11,356
We are returning, whatever
the return value is

971
00:41:11,356 --> 00:41:14,396
of UIApplicationMain
passing in these three things

972
00:41:14,396 --> 00:41:15,856
which don't look
very noteworthy.

973
00:41:16,076 --> 00:41:18,386
The interesting one seems
to be this last one.

974
00:41:18,706 --> 00:41:20,896
That's effectively
passing in a reference

975
00:41:20,896 --> 00:41:24,826
to a class named AppDelegate.

976
00:41:25,176 --> 00:41:29,576
So this one line of code is
what's used in the iOS world

977
00:41:29,956 --> 00:41:33,716
to hand off control, either
briefly or for quite some time,

978
00:41:33,786 --> 00:41:37,566
for main to a different class,
that class being AppDelegate,

979
00:41:37,566 --> 00:41:40,686
and it's apt-- aptly
named in that we are going

980
00:41:40,686 --> 00:41:43,126
to delegate control
of my application

981
00:41:43,126 --> 00:41:46,456
to a guy named AppDelegate,
so that's the class that I,

982
00:41:46,456 --> 00:41:48,796
the programmer, apparently
should start writing

983
00:41:48,796 --> 00:41:50,096
if it doesn't exist just yet.

984
00:41:50,676 --> 00:41:55,586
And as soon as AppDelegate is
done, apparently, main wraps up,

985
00:41:55,686 --> 00:41:58,706
my program quits and
returns by default,

986
00:41:58,706 --> 00:42:00,946
it's not there explicitly, zero.

987
00:42:01,216 --> 00:42:04,026
So this is how we make
this leap from main.m

988
00:42:04,416 --> 00:42:07,696
to whatever my higher
level iOS application is.

989
00:42:07,856 --> 00:42:10,696
So we'll never again really
need to look at this file, you--

990
00:42:11,046 --> 00:42:14,146
can't think of any instances
in the next three weeks

991
00:42:14,146 --> 00:42:15,686
where you'd even want
to touch this file.

992
00:42:15,946 --> 00:42:19,736
So I'm going to close supporting
files all together and leave us

993
00:42:19,736 --> 00:42:21,946
with just two files
now to glance at.

994
00:42:21,946 --> 00:42:22,306
>>What would this mean?

995
00:42:22,516 --> 00:42:25,496
It's a C function, right?

996
00:42:25,496 --> 00:42:29,016
>> UIApplicationMain, it's
a-- sure C function, yes.

997
00:42:29,796 --> 00:42:32,856
Happens to be capitalized but
it's a function, not a method.

998
00:42:33,756 --> 00:42:36,676
All right, so when looking
at files for the first time,

999
00:42:36,676 --> 00:42:40,236
I would generally suggest to
start with the dot H file,

1000
00:42:40,236 --> 00:42:42,096
in part 'cause it's
simpler, usually shorter,

1001
00:42:42,266 --> 00:42:44,066
and also because it's
often more descriptive.

1002
00:42:44,066 --> 00:42:44,846
So let's start there.

1003
00:42:44,846 --> 00:42:47,106
And indeed it is short,
let's see what's going on.

1004
00:42:47,106 --> 00:42:48,576
A whole bunch of
comments at top.

1005
00:42:48,756 --> 00:42:50,826
I'm importing the UIKit again

1006
00:42:50,826 --> 00:42:53,156
so that apparently my
class can have access

1007
00:42:53,156 --> 00:42:54,186
to its functionality.

1008
00:42:54,346 --> 00:42:56,656
And now we see a similar
construct to what you saw

1009
00:42:56,656 --> 00:42:57,706
with Rob last week

1010
00:42:57,706 --> 00:43:02,016
and we recapped a bit ago
@interface AppDelegate colon

1011
00:43:02,016 --> 00:43:05,386
UIResponder open bracket
UIApplicationDelegate

1012
00:43:05,906 --> 00:43:06,606
close bracket.

1013
00:43:07,306 --> 00:43:08,916
So let's try start
translating this

1014
00:43:08,916 --> 00:43:11,066
into slightly less
technical English.

1015
00:43:11,066 --> 00:43:13,116
What does that one
line of code do?

1016
00:43:13,116 --> 00:43:13,186
Yeah?

1017
00:43:14,256 --> 00:43:17,366
>> It says child UIResponder?

1018
00:43:17,366 --> 00:43:19,096
>> OK, it says what's
a child of UIResponder?

1019
00:43:19,096 --> 00:43:19,486
>> The AppDelegate.

1020
00:43:19,906 --> 00:43:22,776
>> OK. It says that the class
called AppDelegate is a child

1021
00:43:22,776 --> 00:43:24,416
of UIResponder.

1022
00:43:24,416 --> 00:43:25,266
Good. And what else?

1023
00:43:25,796 --> 00:43:29,476
>> It adheres to the
UIApplicationDelegate.

1024
00:43:29,476 --> 00:43:31,626
>> And it adheres to the
UIApplicationDelegate,

1025
00:43:31,626 --> 00:43:33,606
and I'll scroll to the
right to confirm protocol.

1026
00:43:33,606 --> 00:43:34,596
So what does that mean?

1027
00:43:34,776 --> 00:43:37,136
It means, not only
does it behave much

1028
00:43:37,136 --> 00:43:40,466
like a UIResponder object
does, no idea what that is,

1029
00:43:40,466 --> 00:43:42,476
but it sounds like something
Apple wrote 'cause it starts

1030
00:43:42,476 --> 00:43:44,536
with UI, part of UIKit.

1031
00:43:44,996 --> 00:43:48,556
And UIApplicationDelegate is
apparently a set of methods

1032
00:43:48,736 --> 00:43:50,856
that Apple says I
have to implement

1033
00:43:51,106 --> 00:43:52,876
if I implement that protocol.

1034
00:43:53,146 --> 00:43:55,166
Now this for the most
part is copy-paste.

1035
00:43:55,216 --> 00:43:58,146
We won't be so presumptuous
as to change these defaults,

1036
00:43:58,416 --> 00:44:01,656
we'll typically isolate
the code that we write else

1037
00:44:01,656 --> 00:44:03,986
to other files or
subsets thereof

1038
00:44:03,986 --> 00:44:05,526
or to new files of our own.

1039
00:44:05,756 --> 00:44:07,586
But let's focus on
one other detail.

1040
00:44:07,636 --> 00:44:11,596
Inside of this class definition
for the AppDelegate class,

1041
00:44:12,146 --> 00:44:13,246
there's also a property.

1042
00:44:14,016 --> 00:44:14,996
What was a property?

1043
00:44:14,996 --> 00:44:15,086
Yeah?

1044
00:44:15,726 --> 00:44:21,056
>> That's the getter and
the setter automatically.

1045
00:44:21,056 --> 00:44:21,476
>> Yeah. Exactly.

1046
00:44:21,476 --> 00:44:25,016
It allowed us to synthesize, so
to speak, a getter and a setter

1047
00:44:25,016 --> 00:44:27,866
for ourselves automatically,
in other words,

1048
00:44:27,866 --> 00:44:30,946
to provide programmatic
access, functional access

1049
00:44:30,946 --> 00:44:34,436
to what's typically an
individual instance variable.

1050
00:44:34,696 --> 00:44:37,196
And what's useful about this is

1051
00:44:37,196 --> 00:44:41,206
that you can specify some
attributes for those setters

1052
00:44:41,206 --> 00:44:43,906
and getters and we'll
see these in more detail,

1053
00:44:43,906 --> 00:44:46,286
before long, but non-atomic.

1054
00:44:46,536 --> 00:44:49,886
Just means, hey Xcode, when you
generate that getter and setter

1055
00:44:49,886 --> 00:44:51,356
for the property called window,

1056
00:44:51,576 --> 00:44:53,866
don't bother writing
thread-safe code,

1057
00:44:53,866 --> 00:44:55,806
and by thread-safe
I mean a whole bunch

1058
00:44:55,806 --> 00:44:58,366
of special synchronization
related code to make sure

1059
00:44:58,366 --> 00:45:01,776
that only one person can call
this function at any given time.

1060
00:45:02,046 --> 00:45:04,566
The reality is, it's
just not going to happen.

1061
00:45:04,566 --> 00:45:07,236
By convention, this does
not need to be atomic code

1062
00:45:07,236 --> 00:45:09,296
and by specifying that
it should be non-atomic,

1063
00:45:09,526 --> 00:45:11,986
it makes the getter
and/or setter faster.

1064
00:45:12,666 --> 00:45:15,626
So we'll-- we won't see in the
coming weeks really an instance

1065
00:45:15,626 --> 00:45:19,686
where you need atomic, and
technically it's there just

1066
00:45:19,686 --> 00:45:22,226
because atomic is the default,
and I want my getter and setter

1067
00:45:22,226 --> 00:45:24,636
to perform ever so slightly
faster even though we humans

1068
00:45:24,636 --> 00:45:25,276
would never notice.

1069
00:45:26,516 --> 00:45:31,336
[ Inaudible Remark ]

1070
00:45:31,836 --> 00:45:33,716
Good question, are
they open source.

1071
00:45:33,716 --> 00:45:35,876
You can see the header
files, the dot H files,

1072
00:45:35,876 --> 00:45:38,946
but to my knowledge not to the
actual implementation files,

1073
00:45:38,946 --> 00:45:40,916
at least not from
recent versions of iOS.

1074
00:45:41,406 --> 00:45:44,006
There may very well be older
versions floating around.

1075
00:45:44,416 --> 00:45:46,546
All we get is the compiled
versions of those files

1076
00:45:46,546 --> 00:45:47,916
and the header files
which are readable.

1077
00:45:47,916 --> 00:45:48,626
All right.

1078
00:45:49,436 --> 00:45:49,786
>> Question.

1079
00:45:49,996 --> 00:45:50,086
>> Yes.

1080
00:45:50,086 --> 00:45:51,346
>> Can you give us an example

1081
00:45:51,346 --> 00:45:55,456
of when you would have
to use the atomic?

1082
00:45:55,766 --> 00:45:57,096
>> If you are writing a--

1083
00:45:57,096 --> 00:45:59,616
when would you use
atomic as an attribute?

1084
00:45:59,616 --> 00:46:03,026
If you are writing
code that require--

1085
00:46:03,026 --> 00:46:05,996
that supports multiple threads,
so two things happening at once,

1086
00:46:06,366 --> 00:46:11,686
and each of those threads might
want to access some value.

1087
00:46:11,686 --> 00:46:15,396
So, let me contrive
an example offhand.

1088
00:46:15,396 --> 00:46:18,016
So, iOS now supports
printing capabilities.

1089
00:46:18,446 --> 00:46:21,426
So for instance, if you had
some program that allows you

1090
00:46:21,426 --> 00:46:22,896
to print your notes
for instance,

1091
00:46:22,896 --> 00:46:23,916
in the notes application.

1092
00:46:24,406 --> 00:46:26,616
If the notes application
were multithreaded,

1093
00:46:26,616 --> 00:46:29,566
so could do two things at once,
and one of those things happen

1094
00:46:29,566 --> 00:46:33,186
to be printing and another
thing happen to be editing,

1095
00:46:33,186 --> 00:46:35,146
which surely the notes
application supports.

1096
00:46:35,626 --> 00:46:38,806
If the printing application,
the printing feature,

1097
00:46:38,946 --> 00:46:41,656
needed to read the contents
of your note, the contents

1098
00:46:41,656 --> 00:46:43,226
of your note might
be implemented by way

1099
00:46:43,226 --> 00:46:45,756
of an instance variable and
accessed by way of a getter

1100
00:46:45,756 --> 00:46:47,266
and setter implemented
as a property.

1101
00:46:47,746 --> 00:46:49,876
Meanwhile, I the
human, with my fingers,

1102
00:46:49,876 --> 00:46:52,256
might want to actually
be editing that same note

1103
00:46:52,256 --> 00:46:54,256
and therefore the editing
feature might also need

1104
00:46:54,256 --> 00:46:57,986
to read and/or write, get and/or
set that instance variable

1105
00:46:58,046 --> 00:46:58,966
through this property.

1106
00:46:59,516 --> 00:47:01,866
And it's probably
not a good thing

1107
00:47:02,026 --> 00:47:04,326
if I allow both the
printing functionality

1108
00:47:04,326 --> 00:47:07,786
and the editing functionality
to look at the value and change

1109
00:47:07,786 --> 00:47:09,026
that value at the same time.

1110
00:47:09,186 --> 00:47:11,936
Because I might say print
now, make some changes,

1111
00:47:11,936 --> 00:47:14,426
and all of a sudden, my
printer starts printing out part

1112
00:47:14,426 --> 00:47:16,196
of the old version and
part of the new version,

1113
00:47:16,386 --> 00:47:20,286
unless I've somehow
locked access to that file.

1114
00:47:20,466 --> 00:47:24,246
So it's contrived in the sense
that you can do things so fast

1115
00:47:24,246 --> 00:47:26,016
that humans-- that's
not going to happen,

1116
00:47:26,016 --> 00:47:28,256
and you can make a copy in
theory of the instance variable

1117
00:47:28,256 --> 00:47:29,546
and avoid that problem
all together,

1118
00:47:29,876 --> 00:47:30,896
but that's the spirit of it.

1119
00:47:30,896 --> 00:47:32,656
If two things might
simultaneously try

1120
00:47:32,656 --> 00:47:36,046
to read and/or write the same
value, bad things can happen,

1121
00:47:36,046 --> 00:47:37,496
like garbled printouts
in that case.

1122
00:47:38,096 --> 00:47:38,266
Yes.

1123
00:47:39,516 --> 00:47:43,336
[ Inaudible Remark ]

1124
00:47:43,836 --> 00:47:44,686
Read-only.

1125
00:47:45,676 --> 00:47:46,956
Read-only is another.

1126
00:47:47,956 --> 00:47:49,916
Read-only gives you a
getter but no setter.

1127
00:47:50,756 --> 00:47:51,476
Good question.

1128
00:47:51,646 --> 00:47:53,746
And strong, this one is easy--

1129
00:47:53,746 --> 00:47:57,406
more easily explained in
the future after some number

1130
00:47:57,406 --> 00:48:00,366
of examples because it refers to
memory management, in particular

1131
00:48:00,366 --> 00:48:02,336
that automatic reference
counting feature

1132
00:48:02,336 --> 00:48:03,496
that I alluded to earlier.

1133
00:48:03,786 --> 00:48:05,686
And we'll see this in
the context of some GUIs

1134
00:48:05,716 --> 00:48:07,796
that we actually
start programming.

1135
00:48:08,396 --> 00:48:10,606
So well that will--
we will revisit.

1136
00:48:11,406 --> 00:48:13,136
All right, so applica--

1137
00:48:13,136 --> 00:48:15,706
AppDelegate.m is the last
place for this program.

1138
00:48:15,706 --> 00:48:18,916
And recall that this program
only loaded a white screen.

1139
00:48:19,016 --> 00:48:21,136
So all of this effort just
to load a white screen,

1140
00:48:21,136 --> 00:48:22,436
but we'll see in
our next example

1141
00:48:22,436 --> 00:48:23,556
that you can do much more

1142
00:48:23,716 --> 00:48:26,426
with relatively little
additional effort.

1143
00:48:26,816 --> 00:48:29,066
So this file is a
little overwhelming,

1144
00:48:29,106 --> 00:48:30,726
but thanks to the
green lines here,

1145
00:48:30,726 --> 00:48:31,996
turns out it's mostly comments.

1146
00:48:31,996 --> 00:48:34,366
So it's just boilerplate
code that Apple provides

1147
00:48:34,566 --> 00:48:36,956
and we can actually delete
most of this just to simplify.

1148
00:48:37,226 --> 00:48:39,496
So let me zoom out just to
get the lay of the land.

1149
00:48:39,496 --> 00:48:41,596
On the top of the file
we have some comments.

1150
00:48:41,926 --> 00:48:44,056
I'm importing AppDelegate.h,

1151
00:48:44,166 --> 00:48:47,646
so AppDelegate.m is importing
AppDelegate.h, common paradigm

1152
00:48:47,646 --> 00:48:50,686
for the methods file to
import the header file.

1153
00:48:51,366 --> 00:48:54,366
@implementation AppDelegate,
what does this mean?

1154
00:48:54,366 --> 00:48:55,746
Translate that one line of code.

1155
00:48:56,566 --> 00:48:58,216
What's that line of
code telling Xcode?

1156
00:48:59,866 --> 00:48:59,976
Yeah?

1157
00:49:00,456 --> 00:49:02,456
[ Inaudible Remark ]

1158
00:49:02,896 --> 00:49:05,016
Exactly. Here comes
the implementation

1159
00:49:05,016 --> 00:49:08,236
of the class called
AppDelegate which was declared

1160
00:49:08,236 --> 00:49:12,186
in the file called
AppDelegate.h. So this func--

1161
00:49:12,186 --> 00:49:15,936
this method is hugely
named application--

1162
00:49:16,076 --> 00:49:18,716
actually, good opportunity
for review.

1163
00:49:18,776 --> 00:49:20,776
What is the name of this method?

1164
00:49:21,876 --> 00:49:25,136
Or more precisely, what is
the name of the selector?

1165
00:49:27,056 --> 00:49:27,206
Yes?

1166
00:49:28,516 --> 00:49:33,076
[ Inaudible Remark ]

1167
00:49:33,576 --> 00:49:34,086
Good, good.

1168
00:49:34,526 --> 00:49:36,616
And as humans we typically
wouldn't say the colons

1169
00:49:36,616 --> 00:49:37,686
but that's exactly right.

1170
00:49:37,686 --> 00:49:39,496
They're part of the
name of the method.

1171
00:49:39,676 --> 00:49:41,366
So, applicationDidFinish

1172
00:49:41,366 --> 00:49:42,556
LaunchingWithOption.

1173
00:49:42,556 --> 00:49:45,656
So, Objective-C recall supports
essentially some variants

1174
00:49:45,656 --> 00:49:48,516
of named parameters where
they're not named in the sense

1175
00:49:48,516 --> 00:49:50,746
that they appear inside of
parenthesis as might be common

1176
00:49:50,746 --> 00:49:53,706
as key value pairs or as
JSON objects in the world

1177
00:49:53,706 --> 00:49:54,966
of JavaScript programming.

1178
00:49:55,366 --> 00:49:59,486
But the names of methods in
Objective-C are meant in spirit

1179
00:49:59,486 --> 00:50:02,086
to read like, frankly
full out sentences,

1180
00:50:02,086 --> 00:50:03,746
so they do exactly
what they say.

1181
00:50:04,226 --> 00:50:05,336
So, what does this one do?

1182
00:50:05,536 --> 00:50:07,646
I honestly have no
idea at firs glance.

1183
00:50:07,646 --> 00:50:09,436
It seems to be code
that someone else wrote.

1184
00:50:09,436 --> 00:50:11,596
Indeed Apple put it in
this template for me.

1185
00:50:11,916 --> 00:50:14,496
But then there's a few lines
of implementation inside of it.

1186
00:50:14,496 --> 00:50:15,566
Let's take a look here.

1187
00:50:15,816 --> 00:50:21,606
Self.window gets UIWindow
alloc initWithFrame UIScreen

1188
00:50:21,606 --> 00:50:22,806
mainScreen bounds.

1189
00:50:23,236 --> 00:50:23,516
All right.

1190
00:50:23,516 --> 00:50:25,636
This looks like probably
the most complex line

1191
00:50:25,636 --> 00:50:27,366
of Objective-C code
we've seen thus far,

1192
00:50:27,536 --> 00:50:29,436
but it's really just
the combination

1193
00:50:29,506 --> 00:50:31,276
of very simple building blocks.

1194
00:50:31,586 --> 00:50:33,756
So someone on the
left hand side,

1195
00:50:34,626 --> 00:50:37,886
what is self.window
referring to?

1196
00:50:38,396 --> 00:50:39,076
What is that?

1197
00:50:39,226 --> 00:50:39,806
>> It is a window.

1198
00:50:40,346 --> 00:50:42,546
>> It's referring to
these programs Window.

1199
00:50:42,546 --> 00:50:45,296
And how about more
technically underneath the hood?

1200
00:50:45,686 --> 00:50:49,976
What is self.window,
W-I-N-D-O-W, referring to?

1201
00:50:50,016 --> 00:50:51,116
[ Inaudible Remark ]

1202
00:50:51,116 --> 00:50:52,256
Close.

1203
00:50:52,256 --> 00:50:53,096
>> The property.

1204
00:50:53,096 --> 00:50:53,756
>> The property.

1205
00:50:54,166 --> 00:50:56,666
So Window, recall, was
the name of the property

1206
00:50:56,666 --> 00:50:57,616
that we saw on what file?

1207
00:50:58,056 --> 00:50:59,246
>> AppDelegate.h.

1208
00:50:59,476 --> 00:51:03,026
>> AppDelegate.h. So we declare
the property called Window.

1209
00:51:03,026 --> 00:51:04,456
And that property
was of what type?

1210
00:51:04,996 --> 00:51:05,306
>> UIWindow.

1211
00:51:05,666 --> 00:51:08,916
>> It was of type UIWindow,
capital U-I-W window.

1212
00:51:09,176 --> 00:51:11,676
OK, and it was a star which
means a pointer there, too.

1213
00:51:12,096 --> 00:51:16,046
So self.window and again,
for those following along

1214
00:51:16,046 --> 00:51:18,486
at home here with laptops, feel
free to download the source code

1215
00:51:18,486 --> 00:51:20,886
from the [inaudible] website
so you can toggle back

1216
00:51:20,886 --> 00:51:22,386
and forth among these
files if you would like.

1217
00:51:22,676 --> 00:51:26,616
So self.window is
effectively invoking a method--

1218
00:51:27,436 --> 00:51:28,016
which method?

1219
00:51:28,546 --> 00:51:28,676
>> Setter.

1220
00:51:29,876 --> 00:51:30,716
>> The setter.

1221
00:51:31,016 --> 00:51:34,306
So this is one of these
pieces of syntactic sugar

1222
00:51:34,306 --> 00:51:36,066
that you get with Objective-C.

1223
00:51:36,366 --> 00:51:38,556
We could implement
this same line of code

1224
00:51:38,556 --> 00:51:44,186
by literally calling set
window as the setter method

1225
00:51:44,536 --> 00:51:46,886
that we get automatically
by declaring a property.

1226
00:51:47,116 --> 00:51:49,036
But one of the purposes
of properties

1227
00:51:49,036 --> 00:51:51,446
in Objective-C is just
to simplify our code

1228
00:51:51,596 --> 00:51:53,676
so that we don't have these
damn square brackets all

1229
00:51:53,676 --> 00:51:55,746
over the place in very
traditional method calls.

1230
00:51:55,996 --> 00:51:58,536
It's just so much more
readable just like in JavaScript

1231
00:51:58,736 --> 00:52:01,816
to have this dot notation and
then an assignment operator,

1232
00:52:01,816 --> 00:52:03,276
the equal sign right next to it

1233
00:52:03,416 --> 00:52:05,306
which effectively
invokes that setter.

1234
00:52:05,336 --> 00:52:07,586
But it's syntactic sugar in
that underneath the hood,

1235
00:52:07,586 --> 00:52:09,346
this is just a message
being passed

1236
00:52:09,926 --> 00:52:12,076
to an object to set some value.

1237
00:52:12,456 --> 00:52:14,226
But it reads much
more clearly this way.

1238
00:52:14,226 --> 00:52:16,816
Self.window is going to
be assigned what value?

1239
00:52:17,126 --> 00:52:19,456
All right, so will someone
else, what does this chunk

1240
00:52:19,456 --> 00:52:21,936
of highlighted code do for us?

1241
00:52:22,286 --> 00:52:24,286
[ Inaudible Remark ]

1242
00:52:24,556 --> 00:52:26,976
OK, it allocates
a UIWindow object.

1243
00:52:26,976 --> 00:52:29,626
I don't really know what that
is just yet but it sounds

1244
00:52:29,626 --> 00:52:30,696
like something that's
useful to have,

1245
00:52:30,696 --> 00:52:33,136
a window for my application
so I'm allocating it.

1246
00:52:33,376 --> 00:52:34,856
I'm not calling init though,

1247
00:52:35,086 --> 00:52:37,316
I'm calling a slightly
differently named func--

1248
00:52:37,456 --> 00:52:42,486
method, initWithFrame, and this
again is largely a human iOS

1249
00:52:42,486 --> 00:52:44,176
convention or Objective-C
convention.

1250
00:52:44,396 --> 00:52:46,766
Init is typically the
default method that you call

1251
00:52:46,766 --> 00:52:47,936
to initialize an object.

1252
00:52:48,066 --> 00:52:49,586
But if it makes sense
programmatically

1253
00:52:49,586 --> 00:52:52,116
to initialize an object
with some arguments,

1254
00:52:52,386 --> 00:52:55,706
the convention is to call it
I-N-I-T then capital letter

1255
00:52:55,826 --> 00:52:58,246
of whatever it is you're
adding to the object.

1256
00:52:58,246 --> 00:52:59,226
So use [inaudible] case.

1257
00:52:59,576 --> 00:53:02,836
And initWithFrame, I don't
know what a frame really is

1258
00:53:02,836 --> 00:53:04,846
but it seems to be
related to the screen

1259
00:53:04,846 --> 00:53:06,076
and the screen's bound.

1260
00:53:06,076 --> 00:53:07,966
So let's look to the
right hand side there.

1261
00:53:08,226 --> 00:53:11,786
The value of this
argument, so to speak,

1262
00:53:11,846 --> 00:53:13,296
is going to be the
result of what?

1263
00:53:13,756 --> 00:53:16,196
Calling UIScreen mainScreen.

1264
00:53:16,486 --> 00:53:18,046
So I'm not calling alloc here.

1265
00:53:18,306 --> 00:53:22,606
Turns out this is a class
method called mainScreen

1266
00:53:22,846 --> 00:53:24,906
that apparently, if we
read the documentation,

1267
00:53:24,906 --> 00:53:28,376
is returning effectively
a pointer to an object

1268
00:53:28,516 --> 00:53:32,586
of type UIScreen that represents
the only screen on my device.

1269
00:53:33,226 --> 00:53:35,066
So the screen is-- you
think of it as the glass.

1270
00:53:35,666 --> 00:53:37,726
Meanwhile, that glass
is a fixed size,

1271
00:53:38,076 --> 00:53:40,836
so when I pass the
message called bounds

1272
00:53:41,226 --> 00:53:44,516
to this UIScreen object, what
do you think bounds returns?

1273
00:53:45,976 --> 00:53:46,416
>> Dimensions.

1274
00:53:46,416 --> 00:53:47,256
>> Yeah, the dimensions.

1275
00:53:47,256 --> 00:53:49,276
The height and the width,
effectively, which is going

1276
00:53:49,276 --> 00:53:51,886
to be a little different if
it's an iPhone3 or an iPhone 4S

1277
00:53:51,886 --> 00:53:54,386
or an iPhone5, depends on
the device, or an iPad,

1278
00:53:54,386 --> 00:53:56,026
depends on the device
potentially.

1279
00:53:56,356 --> 00:54:01,406
And so this is a programmatic
way of getting myself a UIWindow

1280
00:54:01,406 --> 00:54:02,896
which we'll see is
like a container.

1281
00:54:02,986 --> 00:54:04,436
It's just a special UIView.

1282
00:54:04,436 --> 00:54:07,986
It's the parent of all
other view related objects

1283
00:54:07,986 --> 00:54:09,106
on my application.

1284
00:54:09,376 --> 00:54:11,486
I'm just initializing it
dynamically so I don't even have

1285
00:54:11,486 --> 00:54:14,416
to know in advance what
size of screen this is going

1286
00:54:14,416 --> 00:54:15,776
to support necessarily.

1287
00:54:16,936 --> 00:54:19,116
So, a long winded way

1288
00:54:19,116 --> 00:54:22,316
of explaining just how
you get access to a Window

1289
00:54:22,476 --> 00:54:24,136
which again is going
to be a big rectangle

1290
00:54:24,136 --> 00:54:26,806
that we can start putting all
of our buttons and text fields

1291
00:54:26,806 --> 00:54:29,746
and keyboards and all
of that on the screen.

1292
00:54:29,896 --> 00:54:31,006
Now, this is just a comment.

1293
00:54:31,006 --> 00:54:34,606
Override point for customization
after application launch.

1294
00:54:34,866 --> 00:54:37,316
So this is just someone at Apple
wrote this comment for the rest

1295
00:54:37,316 --> 00:54:38,376
of the world to just
kind of know

1296
00:54:38,376 --> 00:54:40,706
where we could start
plugging in code of our own.

1297
00:54:40,706 --> 00:54:41,646
And we'll get to that.

1298
00:54:41,816 --> 00:54:43,186
But now, let's see
what happens after.

1299
00:54:43,186 --> 00:54:46,006
Why is this program's
background white?

1300
00:54:46,816 --> 00:54:48,726
Apparently 'cause
of line three here,

1301
00:54:49,246 --> 00:54:54,986
self.window.backgroundColor
gets UIColor whiteColor.

1302
00:54:55,426 --> 00:54:57,276
So here, too, we have
some conventions.

1303
00:54:57,276 --> 00:54:59,136
Self.window is the property.

1304
00:54:59,546 --> 00:55:02,366
That's referring to my window
object, whatever that is.

1305
00:55:02,366 --> 00:55:05,946
It's a rectangle, a canvas on
which we'll paint other objects.

1306
00:55:06,326 --> 00:55:08,756
Dot backgroundColor,
this too is a property,

1307
00:55:09,016 --> 00:55:12,916
and I know that because
of the fact

1308
00:55:13,086 --> 00:55:14,846
that there's a dot
there, honestly.

1309
00:55:14,846 --> 00:55:17,626
That's my clue that this is
another property associated

1310
00:55:17,626 --> 00:55:18,516
with that object.

1311
00:55:18,896 --> 00:55:23,316
And meanwhile, UIColor I'm
going to guess is a class just

1312
00:55:23,316 --> 00:55:24,926
because it's capital
U, capital I,

1313
00:55:24,926 --> 00:55:26,866
capital C that's
following the conventions

1314
00:55:26,866 --> 00:55:28,326
that Apple has for class names.

1315
00:55:29,026 --> 00:55:31,326
What kind of method is
whiteColor apparently?

1316
00:55:31,326 --> 00:55:32,036
>> Class method.

1317
00:55:34,236 --> 00:55:36,266
>> So it appears to
be a class method

1318
00:55:36,586 --> 00:55:40,396
because if UIColor is a class
and we're not calling alloc,

1319
00:55:40,696 --> 00:55:42,826
the only other way
to talk to a class

1320
00:55:42,826 --> 00:55:45,606
without having an object is
through a class method so you--

1321
00:55:45,606 --> 00:55:49,666
or whiteColor is apparently
a predefined message

1322
00:55:49,666 --> 00:55:51,976
that I can send to
the UIColor class

1323
00:55:51,976 --> 00:55:54,326
and get back some
representation of white

1324
00:55:54,326 --> 00:55:57,366
and maybe it's effectively
a number like FFFFFF.

1325
00:55:57,366 --> 00:56:00,016
It's something like that that
represents the color white,

1326
00:56:00,016 --> 00:56:01,386
however Apple implements it.

1327
00:56:01,586 --> 00:56:05,036
Now, I'm getting a little
tired of taking myself for--

1328
00:56:05,736 --> 00:56:08,486
taking my own word for it,
so let me try a little trick

1329
00:56:08,486 --> 00:56:10,026
of Xcode here to
start digging in.

1330
00:56:10,026 --> 00:56:13,656
If I go and hover over
UIColor, let me zoom back in,

1331
00:56:13,916 --> 00:56:16,906
while holding option, notice
that this is clearly a keyword

1332
00:56:16,906 --> 00:56:19,346
of interest and notice
that my cursor has become a

1333
00:56:19,346 --> 00:56:20,096
question mark.

1334
00:56:20,376 --> 00:56:22,656
So this is one of the nice
features honestly about X code.

1335
00:56:22,656 --> 00:56:25,676
It's probably of all the IDEs
I've used, Elipse and NetBeans

1336
00:56:25,676 --> 00:56:28,356
and others, it's honestly
probably the best.

1337
00:56:28,356 --> 00:56:30,316
It's unfortunately
pretty much designed only

1338
00:56:30,316 --> 00:56:31,646
for Apple programming.

1339
00:56:31,966 --> 00:56:33,516
But it's a really
nice IDE in part

1340
00:56:33,516 --> 00:56:34,676
because it's very instructive.

1341
00:56:34,676 --> 00:56:37,856
So if I now click on
UIColor, notice that I'm going

1342
00:56:37,856 --> 00:56:40,126
to get a little pop-up and
a lot of IDEs have this,

1343
00:56:40,386 --> 00:56:42,786
so this isn't something too new,
but I get a nice description.

1344
00:56:42,786 --> 00:56:44,706
A UIColor object
represents color

1345
00:56:44,706 --> 00:56:47,106
and sometimes opacity,
dot, dot, dot.

1346
00:56:47,106 --> 00:56:48,686
It's been available
for quite some time

1347
00:56:48,776 --> 00:56:50,956
since iOS 2, so several
years ago.

1348
00:56:51,116 --> 00:56:53,866
It's declared in a file
called UIColor.h. So an answer

1349
00:56:53,866 --> 00:56:56,036
to your question, you can't
see the actual implementation

1350
00:56:56,036 --> 00:56:58,746
of this but I can see the
header file and any constants

1351
00:56:58,746 --> 00:57:00,836
or methods or other
things related.

1352
00:57:01,216 --> 00:57:02,616
Here's what's of interest.

1353
00:57:02,716 --> 00:57:06,066
The Apple documentation is
actually pretty darn good.

1354
00:57:06,066 --> 00:57:08,606
So if I click on
UI class reference,

1355
00:57:08,606 --> 00:57:12,726
UIColor class reference, this
program called organizer loads,

1356
00:57:12,916 --> 00:57:15,296
but the same-- this is
just an embedded webpage.

1357
00:57:15,296 --> 00:57:16,786
It's an embedded Safari instance

1358
00:57:16,986 --> 00:57:20,216
so the same documentation is
available on Google and just

1359
00:57:20,216 --> 00:57:23,866
to confirm as much, let
me do UIColor reference.

1360
00:57:24,666 --> 00:57:27,566
Sure enough, first hit is
on developer.apple.com,

1361
00:57:27,816 --> 00:57:29,276
and that's the same exact page.

1362
00:57:29,276 --> 00:57:31,536
So realize you don't even
need X code to poke around.

1363
00:57:31,776 --> 00:57:34,946
So let me increase the font
size and poke around here.

1364
00:57:35,186 --> 00:57:36,986
There's a few things
to keep in mind

1365
00:57:36,986 --> 00:57:38,636
when navigating Apple's
documentation,

1366
00:57:38,636 --> 00:57:39,886
and we won't do this ad nauseam.

1367
00:57:39,886 --> 00:57:41,946
Let's just use UIColor
as a simple example

1368
00:57:41,946 --> 00:57:43,066
of how to navigate the docs.

1369
00:57:43,506 --> 00:57:44,876
It inherits from NSObject.

1370
00:57:45,046 --> 00:57:45,776
So that's kind of cool.

1371
00:57:45,776 --> 00:57:47,006
We've seen how to
do that in code.

1372
00:57:47,006 --> 00:57:48,766
This is just the documentation's
way of saying it.

1373
00:57:49,206 --> 00:57:52,236
Conforms to-- conforms to what?

1374
00:57:52,236 --> 00:57:55,036
What's-- what are those
three blue words apparently

1375
00:57:56,116 --> 00:57:56,946
examples of?

1376
00:57:57,276 --> 00:57:58,556
Those are three protocols.

1377
00:57:58,596 --> 00:58:01,706
NSCopying we've seen, turns out
there's other things, NSCoding,

1378
00:58:01,856 --> 00:58:02,866
NSObject, whatever that is.

1379
00:58:02,866 --> 00:58:04,306
If I clicked on them,
I could learn more,

1380
00:58:04,426 --> 00:58:06,086
but I'm not too interested
in that just now.

1381
00:58:06,356 --> 00:58:08,406
This is part of the
UIKit framework,

1382
00:58:08,406 --> 00:58:10,856
but we kind of guessed that
earlier from the UI prefix.

1383
00:58:11,226 --> 00:58:13,356
Declared in, all right,
somewhat interesting.

1384
00:58:13,526 --> 00:58:14,986
Sample code, so this is

1385
00:58:14,986 --> 00:58:16,796
where I've never been
a fan of Apple's code.

1386
00:58:16,796 --> 00:58:20,536
A lot of their sample projects
are way overcomplicated

1387
00:58:20,536 --> 00:58:21,416
for newbies.

1388
00:58:21,716 --> 00:58:25,116
So I would generally advise
not diving into something

1389
00:58:25,116 --> 00:58:28,116
like a courts demo which is a
fairly sophisticated graphics

1390
00:58:28,116 --> 00:58:31,086
demonstration and looking
instead to online tutorials

1391
00:58:31,086 --> 00:58:33,066
or books or the course's
own sample code,

1392
00:58:33,066 --> 00:58:36,016
'cause I think you'll often
find Apple's code overwhelming.

1393
00:58:36,766 --> 00:58:38,346
But feel free to poke
around if you'd like.

1394
00:58:38,806 --> 00:58:40,586
And now, here's some
standard vanilla stuff.

1395
00:58:40,586 --> 00:58:42,736
So, some English prose
explaining what it is.

1396
00:58:42,736 --> 00:58:46,626
For some reason, Apple
calls methods tasks

1397
00:58:46,676 --> 00:58:49,526
in the documentation but
tasks just means methods.

1398
00:58:49,856 --> 00:58:50,866
The ones that are prefixed

1399
00:58:50,866 --> 00:58:53,216
with a plus means they
are what types of method?

1400
00:58:54,466 --> 00:58:55,136
Class methods.

1401
00:58:55,136 --> 00:58:57,346
You can call them without
instantiating an object.

1402
00:58:57,346 --> 00:59:01,326
With alloc, the minus symbol
[inaudible] fix means it's an

1403
00:59:01,326 --> 00:59:02,076
instance method.

1404
00:59:02,076 --> 00:59:03,946
So you have to allocate
the object first.

1405
00:59:04,116 --> 00:59:07,136
Apple does not exactly
alphabetize them.

1406
00:59:07,136 --> 00:59:09,806
They essentially categorize
them into some arbitrary

1407
00:59:09,806 --> 00:59:13,456
but generally helpful categories
creating a UIColor object

1408
00:59:13,456 --> 00:59:15,176
from component values
initializing.

1409
00:59:15,416 --> 00:59:18,226
So honestly, Command-F will
be your friend quite often.

1410
00:59:18,226 --> 00:59:20,016
When you're looking for
something that you remember part

1411
00:59:20,016 --> 00:59:22,256
of the name but you
can't quite think of it,

1412
00:59:22,256 --> 00:59:23,926
reading through the
documentation can be a little

1413
00:59:23,926 --> 00:59:26,406
tedious because of the
arbitrary way they sort and,

1414
00:59:26,646 --> 00:59:28,546
I mean there is absolutely
no sort order

1415
00:59:28,546 --> 00:59:30,196
to this thing is
best that I can tell.

1416
00:59:30,406 --> 00:59:31,556
But there is white color.

1417
00:59:31,806 --> 00:59:35,376
So if we click on white color,
notice that it's also a plus,

1418
00:59:35,446 --> 00:59:36,386
so it's not a property.

1419
00:59:36,386 --> 00:59:38,746
It's a method that gets
passed to an object,

1420
00:59:38,746 --> 00:59:40,446
message that gets
passed to an object

1421
00:59:40,666 --> 00:59:42,636
and there is its
documentation and let's treat--

1422
00:59:42,636 --> 00:59:44,996
consider this as representative
of the rest of the docs.

1423
00:59:45,556 --> 00:59:48,736
Returns the color object
whose gray scale value is 1.0

1424
00:59:48,736 --> 00:59:52,456
and whose alpha value which
refers to opacity is also 1.0.

1425
00:59:52,456 --> 00:59:55,166
So that's the FFFFFF that
I referred to you earlier.

1426
00:59:55,596 --> 01:00:00,056
Return value of this method
is the UIColor object.

1427
01:00:00,686 --> 01:00:01,966
Where it's available,
not too interesting.

1428
01:00:02,296 --> 01:00:05,466
And then we get yellow and
some other standard colors.

1429
01:00:05,466 --> 01:00:08,306
And if we really wanted to, we
could mix together any colors

1430
01:00:08,306 --> 01:00:10,706
of the rainbow that we want,
but you get some standard ones

1431
01:00:10,706 --> 01:00:13,576
out of the box just 'cause
it's convenient, nothing else.

1432
01:00:14,836 --> 01:00:17,426
So, just a couple of lines
left before we are indeed

1433
01:00:17,426 --> 01:00:18,366
iOS programmers.

1434
01:00:18,446 --> 01:00:20,176
So, let's zoom back in.

1435
01:00:21,146 --> 01:00:25,716
This last line before the
return statement is important

1436
01:00:25,716 --> 01:00:28,206
and you can probably
maybe guess what it is,

1437
01:00:28,986 --> 01:00:31,446
self.window makeKeyAndVisible.

1438
01:00:31,516 --> 01:00:33,916
So first, what is
makeKeyAndVisible?

1439
01:00:34,446 --> 01:00:38,386
Not what it does,
just what is it?

1440
01:00:38,436 --> 01:00:41,986
It's another example of a--
of a message or a method,

1441
01:00:41,986 --> 01:00:45,406
a message that's being passed to
an object or a method invocation

1442
01:00:45,476 --> 01:00:46,396
to put it differently.

1443
01:00:46,726 --> 01:00:47,936
So makeKeyAndVisible.

1444
01:00:47,936 --> 01:00:49,136
I don't really know what key is

1445
01:00:49,176 --> 01:00:52,976
but visible presumably means
make the window visible.

1446
01:00:53,136 --> 01:00:55,856
Perhaps by default it's
hidden, much like you might have

1447
01:00:55,856 --> 01:00:59,566
with CSS hidden divs and
what not in JavaScript,

1448
01:00:59,566 --> 01:01:00,756
so it's apparently
hidden by default.

1449
01:01:00,756 --> 01:01:01,456
We want to show it.

1450
01:01:02,026 --> 01:01:04,206
Key? Key just means
make it the key,

1451
01:01:04,206 --> 01:01:06,236
make it the important
window that responds

1452
01:01:06,236 --> 01:01:08,076
to the user's key
presses by default.

1453
01:01:08,146 --> 01:01:10,336
Make key window makes it
foregrounded effectively.

1454
01:01:11,006 --> 01:01:12,286
So return, and this is one

1455
01:01:12,286 --> 01:01:13,956
of the stupider things
of Objective-C.

1456
01:01:14,346 --> 01:01:16,776
Whereas the rest of the world
uses zero or one or true

1457
01:01:16,776 --> 01:01:19,706
or false, objective C uses
YES or NO, in all caps.

1458
01:01:20,036 --> 01:01:21,386
It's just a Boolean value.

1459
01:01:21,386 --> 01:01:25,356
Return YES means return
success in this particular case.

1460
01:01:26,006 --> 01:01:28,646
Now, thankfully we can
delete the rest of this file

1461
01:01:28,646 --> 01:01:30,976
because down here is a method.

1462
01:01:31,036 --> 01:01:32,056
I'll let someone else answer.

1463
01:01:32,056 --> 01:01:33,726
What's the name of this
highlighted method?

1464
01:01:42,336 --> 01:01:43,236
>> ApplicationWillResignActive
colon UIApplications

1465
01:01:43,236 --> 01:01:43,976
star application.

1466
01:01:43,976 --> 01:01:45,516
>> Too much, you consumed
too much of the string.

1467
01:01:45,516 --> 01:01:47,086
It's just UIWillResignActive.

1468
01:01:47,336 --> 01:01:49,476
And if you're writing it
out, include the colon.

1469
01:01:49,636 --> 01:01:50,716
But the name of this method

1470
01:01:50,716 --> 01:01:52,916
or this message is
ApplicationWillResignActive.

1471
01:01:53,176 --> 01:01:55,486
If I actually read this--
the comments, it will explain

1472
01:01:55,486 --> 01:01:56,616
in more detail what it does.

1473
01:01:56,646 --> 01:01:59,396
But in a nutshell,
this is a method

1474
01:01:59,646 --> 01:02:02,146
that will be invoked
automatically

1475
01:02:02,146 --> 01:02:06,536
by the operating system when
my application, as implemented

1476
01:02:06,536 --> 01:02:08,826
in my AppDelegate class,
is about to do what?

1477
01:02:09,236 --> 01:02:10,506
Resign active state.

1478
01:02:10,666 --> 01:02:12,576
So it's when the user
has hit the button

1479
01:02:12,576 --> 01:02:14,876
but nothing has quite
happened yet.

1480
01:02:15,756 --> 01:02:17,326
So that's my way of responding.

1481
01:02:17,506 --> 01:02:20,856
So think again about
JavaScript and DOM events,

1482
01:02:20,906 --> 01:02:25,496
things like onkeydown,
onkeyup, onsubmit, onchange,

1483
01:02:25,496 --> 01:02:28,316
all of these event handlers,
very similar in spirit,

1484
01:02:28,316 --> 01:02:31,326
whereas those event handlers in
JavaScript are called onsubmit,

1485
01:02:31,326 --> 01:02:35,006
onclick, onkeyup and
so forth, in Object-C,

1486
01:02:35,006 --> 01:02:36,556
they're a little
more-- they're--

1487
01:02:36,556 --> 01:02:39,546
in iOS, they're named
more verbosely

1488
01:02:39,886 --> 01:02:40,746
but it's the same idea.

1489
01:02:40,866 --> 01:02:43,916
These are event handlers
that are being invoked for me

1490
01:02:43,916 --> 01:02:45,036
by the operating system.

1491
01:02:45,356 --> 01:02:45,966
What's another?

1492
01:02:45,966 --> 01:02:47,706
ApplicationDidEnterBackground,

1493
01:02:47,706 --> 01:02:49,486
so when the thing has
actually been backgrounded.

1494
01:02:49,646 --> 01:02:51,296
ApplicationWillEnterForeground.

1495
01:02:51,296 --> 01:02:54,546
So just before it appears
on the screen, maybe I want

1496
01:02:54,546 --> 01:02:57,216
to update the inbox so that the
user sees the current inbox not

1497
01:02:57,246 --> 01:03:00,246
the old one, that what
might have been the case

1498
01:03:00,246 --> 01:03:01,976
when they backgrounded
it a day ago.

1499
01:03:02,226 --> 01:03:05,436
ApplicationDidBecomeActive,
so once it's actually running

1500
01:03:05,436 --> 01:03:07,676
in the foreground,
application will terminate.

1501
01:03:07,676 --> 01:03:10,216
If the user holds down the
button and clicks the X

1502
01:03:10,216 --> 01:03:11,976
on an icon, you do have a chance

1503
01:03:11,976 --> 01:03:14,706
to do some last minute
housekeeping before they even

1504
01:03:14,756 --> 01:03:16,266
force quit their application.

1505
01:03:16,266 --> 01:03:19,636
So these are all hooks into
these very low level operations.

1506
01:03:19,906 --> 01:03:22,786
Notice that there are
comments inside of them

1507
01:03:22,786 --> 01:03:24,206
but nothing is actually
happening,

1508
01:03:24,206 --> 01:03:26,526
I can actually safely
delete all of them.

1509
01:03:26,856 --> 01:03:27,936
That won't break the class

1510
01:03:28,206 --> 01:03:34,566
because what class does
AppDelegate descend from?

1511
01:03:34,746 --> 01:03:35,676
UIResponder.

1512
01:03:35,676 --> 01:03:37,556
So if I actually
dig a little deeper,

1513
01:03:37,556 --> 01:03:39,796
it turns out that those
methods are probably implemented

1514
01:03:39,796 --> 01:03:42,706
elsewhere, higher in this
class hierarchy for me.

1515
01:03:42,966 --> 01:03:44,246
So even though the templates

1516
01:03:44,246 --> 01:03:46,786
that Apple provides often
have lots and lots of code,

1517
01:03:46,786 --> 01:03:49,356
a lot of it just
to kind of focus

1518
01:03:49,546 --> 01:03:51,996
and simplify things you
can start deleting at least

1519
01:03:51,996 --> 01:03:53,476
if there's no built-in
functionality there.

1520
01:03:53,476 --> 01:03:55,136
And in all of the
lecture examples tonight,

1521
01:03:55,366 --> 01:03:56,436
I've done that in advance.

1522
01:03:56,436 --> 01:03:58,896
As you'll see I've pruned away
anything that's superfluous

1523
01:03:58,896 --> 01:04:02,226
so we can only focus on what's
core to those applications.

1524
01:04:02,796 --> 01:04:03,976
So incredibly underwhelming.

1525
01:04:04,126 --> 01:04:06,206
Before we take a break,
let's at least reassure

1526
01:04:06,206 --> 01:04:07,736
that we can do more
interesting things.

1527
01:04:07,736 --> 01:04:09,976
I'm going to go ahead and
rapidly create a new project.

1528
01:04:10,856 --> 01:04:13,866
Single view applications,
'cause this time I want a view,

1529
01:04:13,866 --> 01:04:15,696
I want something
on the damn screen.

1530
01:04:15,906 --> 01:04:17,806
I'm going to call this
Test2, but we'll come back

1531
01:04:17,806 --> 01:04:19,226
with more interesting examples.

1532
01:04:19,546 --> 01:04:21,736
I'm going to uncheck
used storyboards

1533
01:04:21,736 --> 01:04:23,096
for now, more on that to come.

1534
01:04:23,096 --> 01:04:24,356
I'm going to click next.

1535
01:04:24,436 --> 01:04:26,366
I'm going to go ahead and
save it on my desktop.

1536
01:04:27,116 --> 01:04:31,086
I am now going to notice but
explain later a few new files,

1537
01:04:31,086 --> 01:04:33,036
ViewController, that's
good, we have some brains

1538
01:04:33,036 --> 01:04:34,186
for my application now.

1539
01:04:34,446 --> 01:04:42,126
But also this xib file
pronounced of course nib, N-I-B,

1540
01:04:42,206 --> 01:04:43,586
for historical reasons.

1541
01:04:43,586 --> 01:04:46,386
When-- as soon as the nib
file became an XML file,

1542
01:04:46,386 --> 01:04:48,526
they renamed it xib,
but we still say nib.

1543
01:04:48,806 --> 01:04:51,066
Now notice nib or dot xib,

1544
01:04:51,066 --> 01:04:54,426
this makes programming look
fun now all of a sudden.

1545
01:04:54,716 --> 01:04:57,776
This loads what's effectively a
tool called Interface Builder,

1546
01:04:57,776 --> 01:04:59,456
although Apple doesn't
really call it

1547
01:04:59,456 --> 01:05:00,946
that anymore 'cause
it's embedded right

1548
01:05:00,946 --> 01:05:01,976
into Xcode itself.

1549
01:05:02,436 --> 01:05:04,666
This is how easy it's
going to be to start making

1550
01:05:04,666 --> 01:05:07,046
at least the simplest
of application's UIs.

1551
01:05:07,046 --> 01:05:10,366
I'm going to go to my
little palette here,

1552
01:05:10,366 --> 01:05:13,116
on the right hand
side and notice

1553
01:05:13,296 --> 01:05:15,836
that there's some juicy
friendly-looking stuff

1554
01:05:15,836 --> 01:05:18,596
down here, label, rounded
rectangular button,

1555
01:05:18,596 --> 01:05:21,636
segmented control, slider,
switch, progress view,

1556
01:05:21,816 --> 01:05:23,616
so some familiar UI mechanisms.

1557
01:05:23,616 --> 01:05:25,806
I'm going to do a super
simple one like label.

1558
01:05:25,806 --> 01:05:28,176
I'm going to drop it over here,

1559
01:05:28,176 --> 01:05:31,746
roughly in the let's say
top middle of the screen.

1560
01:05:31,886 --> 01:05:34,066
I'm going to say hello class.

1561
01:05:35,116 --> 01:05:36,236
OK, it's still centered.

1562
01:05:36,556 --> 01:05:39,006
Now I'm going to click
the run button at top left

1563
01:05:39,186 --> 01:05:41,966
which will launch the
simulator and if all is well,

1564
01:05:42,006 --> 01:05:45,076
now we have the beginnings of
a more interesting application.

1565
01:05:45,076 --> 01:05:46,716
So let's take a five-minute
break here, we'll come back

1566
01:05:46,716 --> 01:05:48,706
and actually see how
to make that and more.

1567
01:05:49,536 --> 01:05:50,906
All right we are back,

1568
01:05:51,016 --> 01:05:53,916
so the exciting cliffhanger
before we took a break was

1569
01:05:53,916 --> 01:05:56,606
to get hello class
on to the screen.

1570
01:05:56,606 --> 01:05:57,596
But before we forge ahead

1571
01:05:57,596 --> 01:06:00,576
and reveal just how this
magic happens, any questions

1572
01:06:00,576 --> 01:06:03,366
about Objective-C, UIKit
or anything thus far.

1573
01:06:03,996 --> 01:06:07,126
Really? OK.

1574
01:06:07,376 --> 01:06:10,626
>> What is this little bar at
the bottom, the red rectangle,

1575
01:06:10,626 --> 01:06:15,226
red rectangle, green
[inaudible]?

1576
01:06:15,426 --> 01:06:16,596
>>Red rectangle--

1577
01:06:17,016 --> 01:06:18,586
[ Inaudible Remark ]

1578
01:06:18,586 --> 01:06:19,506
On yours.

1579
01:06:19,786 --> 01:06:21,796
>> It's all-- it
should be on hold.

1580
01:06:22,476 --> 01:06:23,906
>> Let me see if I
can replicate that.

1581
01:06:26,046 --> 01:06:30,676
Let's see, how did you
get that on to your--

1582
01:06:30,746 --> 01:06:32,186
you have it at the
bottom of the UIView?

1583
01:06:32,186 --> 01:06:34,296
>> Yeah. It's sitting
on the bottom.

1584
01:06:35,296 --> 01:06:39,906
>> Yeah, it's a good question,
it has to do with hooks.

1585
01:06:39,906 --> 01:06:42,196
They're similar in spirit
to these, which we will talk

1586
01:06:42,196 --> 01:06:45,786
about that allow you to
drag and drop more easily

1587
01:06:45,786 --> 01:06:47,956
on to the surface
of this object.

1588
01:06:48,426 --> 01:06:51,236
We don't have much in this.

1589
01:06:51,236 --> 01:06:52,976
Let's see, click here.

1590
01:06:54,216 --> 01:06:57,746
I am not sure where that
feature has been tucked away,

1591
01:06:57,746 --> 01:07:00,486
I will try to replicate it
after and we can look at it

1592
01:07:00,486 --> 01:07:01,386
at your-- on your screen.

1593
01:07:01,476 --> 01:07:02,006
OK. Yup?

1594
01:07:03,976 --> 01:07:07,326
>> I have something called
storyboard instead of--

1595
01:07:07,976 --> 01:07:10,626
>> That's because when
you created your project

1596
01:07:11,016 --> 01:07:14,486
for single view application, you
left Use Storyboards checked,

1597
01:07:15,116 --> 01:07:16,126
and we'll come back to that.

1598
01:07:16,126 --> 01:07:18,586
But for now I thought I'd
simplify and uncheck that.

1599
01:07:18,586 --> 01:07:18,676
>> OK.

1600
01:07:18,706 --> 01:07:20,356
>> If you'd like to
re follow those steps.

1601
01:07:21,776 --> 01:07:22,166
All right.

1602
01:07:22,256 --> 01:07:25,646
So we are back now with
the single view application

1603
01:07:25,776 --> 01:07:27,866
and we seem to have a
couple of more files.

1604
01:07:27,866 --> 01:07:31,366
We still have an AppDelegate
we still have an AppDelegate.h

1605
01:07:31,366 --> 01:07:34,646
and .m specifically but we've
got at least three new files.

1606
01:07:34,646 --> 01:07:36,696
And just in case
there's even more going

1607
01:07:36,696 --> 01:07:39,546
on underneath the hood, let's
zoom in on these groups,

1608
01:07:39,546 --> 01:07:40,836
these folders over here.

1609
01:07:41,236 --> 01:07:43,426
Those files look familiar.

1610
01:07:43,426 --> 01:07:45,516
I don't think there's
anything new in there.

1611
01:07:45,516 --> 01:07:47,476
The file is called Test2
instead of Test now

1612
01:07:47,476 --> 01:07:49,036
but that's 'cause I
renamed my project.

1613
01:07:49,296 --> 01:07:50,726
So all of these looks familiar,

1614
01:07:50,816 --> 01:07:53,146
nothing else is new
underneath the hood.

1615
01:07:53,146 --> 01:07:56,336
And indeed if we look at main.m,
there's nothing new there,

1616
01:07:56,336 --> 01:07:57,576
it's still called AppDelegate

1617
01:07:57,576 --> 01:07:59,426
and somehow that's
kicking off this process.

1618
01:07:59,426 --> 01:08:01,126
So let's ignore those
supporting files.

1619
01:08:01,406 --> 01:08:04,496
And let's go into AppDelegate.h
to see what's new here.

1620
01:08:04,626 --> 01:08:07,486
Let me hide this window on the
right hand side for simplicity.

1621
01:08:07,706 --> 01:08:10,236
And does anything jump out
at you as new in this example

1622
01:08:10,236 --> 01:08:12,696
versus the empty
application we wrote first.

1623
01:08:13,176 --> 01:08:16,176
Yeah. There's a view controller.

1624
01:08:16,176 --> 01:08:18,966
So this is interesting, we
have two properties, not one,

1625
01:08:19,256 --> 01:08:22,396
and we needed that original
property window just

1626
01:08:22,396 --> 01:08:23,606
so that we had something to hang

1627
01:08:23,606 --> 01:08:26,896
on to somehow called the
UIWindow and it was assigned

1628
01:08:26,896 --> 01:08:29,086
to that property
in AppDelegate.m.

1629
01:08:29,506 --> 01:08:31,796
But now we've got another
property also strong

1630
01:08:31,796 --> 01:08:35,166
and non-atomic, which for now
just take on faith as copy-paste

1631
01:08:35,166 --> 01:08:36,676
when you declare a
property like this.

1632
01:08:37,066 --> 01:08:40,586
It's apparently a pointer called
viewController, lower case v,

1633
01:08:40,876 --> 01:08:43,456
and it points to an object
of type ViewController,

1634
01:08:43,456 --> 01:08:47,306
capital V. So I don't quite
know what that is in terms

1635
01:08:47,306 --> 01:08:50,896
of iOS's worlds but view
controller, it's a controller

1636
01:08:50,896 --> 01:08:53,776
which was what, in
general terms?

1637
01:08:54,306 --> 01:08:57,246
What's the view controller?

1638
01:08:57,416 --> 01:08:58,306
What's a controller?

1639
01:08:58,746 --> 01:08:58,906
>> Brain.

1640
01:09:01,536 --> 01:09:02,406
>> Brain of the [inaudible].

1641
01:09:02,496 --> 01:09:04,406
>> Yeah, so it's the
brains of the operation.

1642
01:09:04,406 --> 01:09:07,906
So MVC, so recall one paradigm,
let's see-- let's really--

1643
01:09:08,066 --> 01:09:09,426
give me at least
one chance to swipe.

1644
01:09:09,426 --> 01:09:09,916
All right.

1645
01:09:09,916 --> 01:09:13,596
So we had this picture
over here, MVC.

1646
01:09:13,756 --> 01:09:15,646
So, the view controller
in the world

1647
01:09:15,646 --> 01:09:17,836
of iOS is just the c in MVC.

1648
01:09:17,836 --> 01:09:19,666
So it's the brains
of our application.

1649
01:09:19,666 --> 01:09:21,386
Now in reality, as
you get your--

1650
01:09:21,386 --> 01:09:23,806
you start writing more and
more complex applications,

1651
01:09:24,006 --> 01:09:26,836
you might very well have more
than one view controller,

1652
01:09:26,836 --> 01:09:30,096
more than just one C and you
might have a super important

1653
01:09:30,096 --> 01:09:33,196
controller that somehow
delegates control among other

1654
01:09:33,196 --> 01:09:34,816
controllers but it's
still the brains

1655
01:09:34,816 --> 01:09:36,256
of the operation ultimately.

1656
01:09:36,526 --> 01:09:40,846
So let's dive into the H file
as should be common technique

1657
01:09:40,846 --> 01:09:45,426
to just see what's going on
inside of ViewController.h.

1658
01:09:46,056 --> 01:09:47,796
That is very underwhelming.

1659
01:09:48,386 --> 01:09:52,096
So my ViewController class
is apparently a child

1660
01:09:52,096 --> 01:09:57,356
of the UIViewController class
and that's where the story ends.

1661
01:09:57,386 --> 01:09:59,926
There's nothing going on at
least inside of the H file.

1662
01:10:00,326 --> 01:10:03,946
So let's take a look at
AppDelegate.m before we look

1663
01:10:03,946 --> 01:10:04,936
at that other method file.

1664
01:10:05,906 --> 01:10:07,616
What else is new here?

1665
01:10:07,616 --> 01:10:10,076
Well, I'm a little overwhelmed
by all the lines here

1666
01:10:10,076 --> 01:10:12,636
but I realized that, oh, almost
all of these is comments.

1667
01:10:12,946 --> 01:10:15,926
So for simplicity, let me go
ahead and throw away everything

1668
01:10:15,926 --> 01:10:19,506
that looks uninteresting in
this template and zoom in only

1669
01:10:19,506 --> 01:10:20,826
on the code that remains.

1670
01:10:21,276 --> 01:10:23,496
Now this first line,
self.window,

1671
01:10:23,966 --> 01:10:25,356
this looks familiar, right?

1672
01:10:25,356 --> 01:10:28,526
It's a little verbose but
it allocated a UIWindow,

1673
01:10:28,526 --> 01:10:31,606
figured out its dimensions and
gave me a pointer there too

1674
01:10:31,606 --> 01:10:33,356
by storing it in
my window property.

1675
01:10:33,976 --> 01:10:35,076
OK, so something to hang

1676
01:10:35,076 --> 01:10:37,056
on to even though we didn't
do anything interesting

1677
01:10:37,056 --> 01:10:37,576
with it yet.

1678
01:10:38,026 --> 01:10:39,466
Now, what line of code is new

1679
01:10:40,036 --> 01:10:41,876
in this method called
application

1680
01:10:41,876 --> 01:10:43,166
didFinishLaunchingWithOptions?

1681
01:10:45,656 --> 01:10:47,706
Yeah, self.viewController.

1682
01:10:47,706 --> 01:10:49,506
So here is something
interesting.

1683
01:10:49,506 --> 01:10:52,056
Even though this application
only puts hello class

1684
01:10:52,056 --> 01:10:54,636
on the screen, this
line of code looks new.

1685
01:10:54,906 --> 01:10:57,656
So self.viewController
is referring to what?

1686
01:10:57,966 --> 01:11:02,086
To be clear.

1687
01:11:02,496 --> 01:11:02,876
Say again?

1688
01:11:03,016 --> 01:11:05,016
[ Inaudible Remark ]

1689
01:11:05,016 --> 01:11:06,806
The ViewController property.

1690
01:11:06,806 --> 01:11:08,716
So self.viewController
is the property

1691
01:11:08,716 --> 01:11:11,386
that was declared in what file?

1692
01:11:11,566 --> 01:11:13,576
AppDelegate.h, exactly.

1693
01:11:13,576 --> 01:11:16,606
So we're assigning to that
property the return value

1694
01:11:16,606 --> 01:11:17,886
of this expression here.

1695
01:11:18,106 --> 01:11:21,376
So a little bit of this is
new, ViewController alloc.

1696
01:11:21,496 --> 01:11:25,016
So alloc allocates me a new
object of type ViewController.

1697
01:11:25,406 --> 01:11:27,666
Who wrote the ViewController
class?

1698
01:11:28,016 --> 01:11:29,456
[ Inaudible Remark ]

1699
01:11:29,456 --> 01:11:32,956
Apple, at least in the form of
the template code, but they seem

1700
01:11:32,956 --> 01:11:34,846
to have given me
the raw source code

1701
01:11:34,846 --> 01:11:36,976
for the ViewController
class as we just saw

1702
01:11:36,976 --> 01:11:39,086
from the ViewController.h.
So I can take

1703
01:11:39,086 --> 01:11:40,656
over the brains of
this operation.

1704
01:11:40,896 --> 01:11:44,106
They did not give me the source
code for UIViewController.

1705
01:11:44,396 --> 01:11:47,276
But that's fine 'cause my
ViewController class can still

1706
01:11:47,856 --> 01:11:50,056
descend from it in
terms of a hierarchy.

1707
01:11:50,056 --> 01:11:51,276
It can be my parent nonetheless.

1708
01:11:51,836 --> 01:11:54,866
So give me a ViewController
object

1709
01:11:55,026 --> 01:11:57,416
and then initialize
it with nib name.

1710
01:11:57,416 --> 01:11:59,156
So here we see the N for nib

1711
01:11:59,376 --> 01:12:02,086
for again historical
reasons, but nib name.

1712
01:12:02,086 --> 01:12:04,986
A nib is at the end of
the day just an XML file.

1713
01:12:05,016 --> 01:12:07,356
But it's not an XML file
we will ever look at.

1714
01:12:07,356 --> 01:12:08,916
You can but it's
just not interesting

1715
01:12:08,916 --> 01:12:10,186
and it's rather overwhelming.

1716
01:12:10,636 --> 01:12:15,216
A nib file is just a file
that stores the result

1717
01:12:15,216 --> 01:12:18,596
of creating a user interface,
using Xcode by dragging

1718
01:12:18,596 --> 01:12:20,576
and dropping widgets
like I did earlier.

1719
01:12:20,576 --> 01:12:22,606
I drag the simplest
of them just to label

1720
01:12:22,816 --> 01:12:25,866
but I could start dragging
as you saw sliders and knobs

1721
01:12:25,866 --> 01:12:28,406
and switches and text fields
and other such things.

1722
01:12:28,746 --> 01:12:33,136
A nib file just stores that
result of all that dragging

1723
01:12:33,136 --> 01:12:34,966
and dropping so that
you can program

1724
01:12:34,966 --> 01:12:36,596
at least your UI a
little more easily.

1725
01:12:36,596 --> 01:12:39,746
Now you don't have to program
your UI with Xcode in that way.

1726
01:12:39,746 --> 01:12:42,716
We'll see it and do it
in much more manual way.

1727
01:12:42,976 --> 01:12:46,276
But what this does is it
initializes my view controller

1728
01:12:46,446 --> 01:12:50,356
with the contents of a
file called nib name is

1729
01:12:50,356 --> 01:12:51,176
new controller.

1730
01:12:51,396 --> 01:12:54,816
If I read the documentation,
I will see that that method,

1731
01:12:54,816 --> 01:12:57,456
initWithNibName is
supposed to look

1732
01:12:57,456 --> 01:13:03,826
for by default the string
that's in quotes dot XIB.

1733
01:13:04,396 --> 01:13:07,676
So that's how I'm linking
my code in Objective-C

1734
01:13:07,926 --> 01:13:10,806
to what we call the nib file
earlier which again I got

1735
01:13:10,806 --> 01:13:12,376
for free via this template.

1736
01:13:12,376 --> 01:13:14,796
So that's where my
code is saying,

1737
01:13:15,016 --> 01:13:17,046
give me a view controller
and initialize it

1738
01:13:17,046 --> 01:13:18,676
with the contents
of that XML file.

1739
01:13:18,676 --> 01:13:20,806
So in other words if
I wrote, if I created

1740
01:13:20,806 --> 01:13:23,686
that user interface a week ago
by dragging and dropping labels

1741
01:13:23,686 --> 01:13:26,626
and text fields and sliders
a week ago and saved it,

1742
01:13:26,896 --> 01:13:29,686
this reloads it into
the program's memory.

1743
01:13:30,096 --> 01:13:35,976
So, lastly, another new line,
self.window.rootViewController.

1744
01:13:36,266 --> 01:13:37,226
We didn't have this before

1745
01:13:37,226 --> 01:13:38,936
because we didn't
have a controller.

1746
01:13:38,936 --> 01:13:41,846
Our program was so simple there
were no brains behind the scenes

1747
01:13:42,046 --> 01:13:43,746
'cause it only showed
a white screen.

1748
01:13:43,996 --> 01:13:46,486
But it turns out there
is a property called

1749
01:13:46,486 --> 01:13:50,216
rootViewController whose
purpose in life is to serve

1750
01:13:50,216 --> 01:13:53,966
as exactly that, the most
important view controller,

1751
01:13:53,966 --> 01:13:58,576
the most important C controller
that runs the entire operation.

1752
01:13:58,776 --> 01:14:00,156
Now how do I know
that's the case?

1753
01:14:00,156 --> 01:14:02,186
Again, you don't have to
take all of this on faith.

1754
01:14:02,416 --> 01:14:05,126
Let me hover over self dot--

1755
01:14:05,266 --> 01:14:08,286
whoops, let me hover
over self.window,

1756
01:14:08,286 --> 01:14:09,496
a question mark appears.

1757
01:14:09,786 --> 01:14:10,946
And, oh, interesting.

1758
01:14:10,946 --> 01:14:16,586
This is a property called window
that's been declared in there.

1759
01:14:16,746 --> 01:14:18,726
And I want to see the
documentation, so let me go

1760
01:14:18,726 --> 01:14:20,226
to the reference
down at the bottom.

1761
01:14:20,606 --> 01:14:22,676
And now the reference
for the window--

1762
01:14:23,016 --> 01:14:26,866
there's a whole bunch
of stuff, and if I skim,

1763
01:14:26,866 --> 01:14:30,326
skim, skim, skim, root.

1764
01:14:30,876 --> 01:14:36,086
Mine is the only phone connected
to the speakers in the room, OK.

1765
01:14:37,676 --> 01:14:41,486
So if I skim through
here, property is window.

1766
01:14:41,486 --> 01:14:43,176
Oh, you know what, I'm
in the wrong place.

1767
01:14:43,396 --> 01:14:45,636
Because if this is the
UIWindow, I'm currently

1768
01:14:45,636 --> 01:14:48,266
in the UIApplicationDelegate
reference

1769
01:14:48,536 --> 01:14:51,406
which just means again
my AppDelegate implements

1770
01:14:51,866 --> 01:14:52,496
that thing.

1771
01:14:52,826 --> 01:14:54,666
So what I actually
want to go to.

1772
01:14:54,666 --> 01:14:58,296
Let's do that trick
from earlier, UIWindow,

1773
01:14:59,286 --> 01:15:01,646
and there sure enough,
let's make sure we get

1774
01:15:01,736 --> 01:15:04,076
to the iOS version and
not the Mac OS version.

1775
01:15:04,076 --> 01:15:05,506
So let's add iOS there.

1776
01:15:05,986 --> 01:15:07,426
UIWindow class reference.

1777
01:15:08,136 --> 01:15:11,436
And now if I scroll down
tasks which again our methods.

1778
01:15:12,186 --> 01:15:14,716
Keywindow, we haven't seen
that before, but it sounds

1779
01:15:14,716 --> 01:15:16,626
like the buzz word we
saw earlier and aha,

1780
01:15:17,176 --> 01:15:18,876
there is the formal
documentation

1781
01:15:19,066 --> 01:15:23,436
that says a UIWindow, which is
what my window property points

1782
01:15:23,436 --> 01:15:26,626
to, has a property
called rootViewController.

1783
01:15:26,776 --> 01:15:28,026
And if I read the documentation,

1784
01:15:28,026 --> 01:15:29,916
the root view controller
provides the content view

1785
01:15:29,916 --> 01:15:30,596
of the window.

1786
01:15:30,596 --> 01:15:32,936
Assigning a view controller
to this property dat dat da,

1787
01:15:33,146 --> 01:15:34,836
installs the view
controller's view

1788
01:15:34,836 --> 01:15:36,236
as the content view dat dat da.

1789
01:15:36,826 --> 01:15:40,886
This is how you specify who
the brains are behind your

1790
01:15:40,886 --> 01:15:43,796
application, and it's going
to be specifically an instance

1791
01:15:43,796 --> 01:15:45,466
of my view controller class.

1792
01:15:46,416 --> 01:15:49,926
So, the only two lines here
then that are new are these,

1793
01:15:50,546 --> 01:15:53,596
the only files that are
new or the three up there.

1794
01:15:53,776 --> 01:15:55,776
So there's one file
we haven't looked at

1795
01:15:55,776 --> 01:16:01,336
yet which is ViewController.m.

1796
01:16:01,406 --> 01:16:03,256
So the implementation
of this class.

1797
01:16:03,706 --> 01:16:08,086
So looks like fortunately
just some template code.

1798
01:16:08,086 --> 01:16:10,066
And in fact I can
delete all of these.

1799
01:16:10,636 --> 01:16:13,556
So let's see what's in here
of interest potentially.

1800
01:16:13,826 --> 01:16:16,776
I've got a @interface sign

1801
01:16:16,776 --> 01:16:20,196
at the top ViewController
open parens and then end.

1802
01:16:20,416 --> 01:16:23,606
This feels weird because where
do I usually see @interface?

1803
01:16:23,606 --> 01:16:26,626
So in the header file.

1804
01:16:27,026 --> 01:16:28,886
But I already saw this
in the header file.

1805
01:16:28,886 --> 01:16:31,866
Just to be clear, if we go
back to ViewController.h,

1806
01:16:32,286 --> 01:16:35,926
I already have, excuse me,
@interface ViewController.

1807
01:16:36,466 --> 01:16:38,726
So what is this doing here?

1808
01:16:39,516 --> 01:16:42,036
[ Inaudible Remark ]

1809
01:16:42,536 --> 01:16:43,246
What's that?

1810
01:16:43,466 --> 01:16:46,006
Yeah, it's sort of
a private interface.

1811
01:16:46,006 --> 01:16:51,936
It's using what appears to be
a-- what's the buzz word here?

1812
01:16:52,656 --> 01:17:00,556
A feature of the language known
as a category, yet turns out--

1813
01:17:00,556 --> 01:17:02,416
and this was something
added somewhat recently,

1814
01:17:02,416 --> 01:17:05,276
you can have nameless
categories with no name,

1815
01:17:05,276 --> 01:17:06,556
open paren, close paren.

1816
01:17:06,816 --> 01:17:09,126
So this is a category
which just means here--

1817
01:17:09,206 --> 01:17:12,676
hey, Xcode, here comes some
more methods that are associated

1818
01:17:12,676 --> 01:17:14,236
with the ViewController class.

1819
01:17:14,556 --> 01:17:16,646
Now it turns out this
is just boilerplate code

1820
01:17:16,646 --> 01:17:18,246
that Apple has provided
a starting point.

1821
01:17:18,486 --> 01:17:21,956
There are no such methods but if
I wanted to have private methods

1822
01:17:21,986 --> 01:17:23,386
that I don't inform the world

1823
01:17:23,386 --> 01:17:25,586
about via my header file,
where do I put them?

1824
01:17:26,016 --> 01:17:27,696
Where that blank
line is right there.

1825
01:17:27,986 --> 01:17:30,576
But functionally, this
isn't doing anything for me,

1826
01:17:30,576 --> 01:17:31,986
it's kind of overwhelming me.

1827
01:17:31,986 --> 01:17:33,886
So I'm just going to get
rid of it with no effect.

1828
01:17:34,286 --> 01:17:37,396
@implementation is what I did
expect to see in this file,

1829
01:17:37,396 --> 01:17:40,146
and now there're two methods
that we did not see before.

1830
01:17:40,746 --> 01:17:42,876
And they're not arbitrary.

1831
01:17:43,486 --> 01:17:45,516
It turns out that viewDidLoad

1832
01:17:45,786 --> 01:17:49,766
and didReceiveMemoryWarning
are methods defined

1833
01:17:49,766 --> 01:17:53,606
by Apple in what class?

1834
01:17:54,386 --> 01:17:56,456
They're not in my
class because, right,

1835
01:17:56,456 --> 01:17:59,526
if I go to my header
file, I made no commitment

1836
01:17:59,526 --> 01:18:00,946
to implementing those two.

1837
01:18:00,946 --> 01:18:03,126
So where do viewDidLoad

1838
01:18:03,286 --> 01:18:05,486
and didReceiveMemoryWarning
come from?

1839
01:18:05,966 --> 01:18:07,246
Yeah. Yeah.

1840
01:18:07,246 --> 01:18:10,546
Must come from some parent or
ancestor class, and I'm going

1841
01:18:10,546 --> 01:18:13,196
to guess it's from
UIViewController.

1842
01:18:13,336 --> 01:18:15,456
And if we actually looked
at the documentation there,

1843
01:18:15,456 --> 01:18:16,656
maybe followed some links,

1844
01:18:16,656 --> 01:18:19,696
hopefully we would
indeed see the definitions

1845
01:18:19,756 --> 01:18:22,696
or the declarations of those
methods in the documentation.

1846
01:18:22,866 --> 01:18:23,736
Now what do they do?

1847
01:18:23,736 --> 01:18:26,216
Thankfully, there is an
upside to the verboseness

1848
01:18:26,216 --> 01:18:27,426
of all these method names.

1849
01:18:27,636 --> 01:18:31,316
ViewDidLoad, that method is
invoked when the view did load.

1850
01:18:31,466 --> 01:18:33,396
And what's the view,
just to recap?

1851
01:18:34,336 --> 01:18:36,146
What is the view with
respect to an application?

1852
01:18:37,416 --> 01:18:38,186
The interface.

1853
01:18:38,356 --> 01:18:40,316
So when some stuff
came up on the screen,

1854
01:18:40,316 --> 01:18:41,266
what method gets called?

1855
01:18:41,846 --> 01:18:43,766
That one. Why might
that be of interest?

1856
01:18:43,936 --> 01:18:46,226
Well, once the stuff has
been painted on the screen,

1857
01:18:46,226 --> 01:18:48,976
the text fields and the sliders
and whatnot, maybe you want

1858
01:18:48,976 --> 01:18:50,206
to start updating the values.

1859
01:18:50,206 --> 01:18:51,626
Maybe you want to
start bouncing a ball

1860
01:18:51,626 --> 01:18:54,086
around once the UI has been
generated for your game.

1861
01:18:54,086 --> 01:18:56,826
Whatever you might want to do
could happen at that juncture.

1862
01:18:56,826 --> 01:18:59,416
And so Apple's comment here
says, do any additional set

1863
01:18:59,416 --> 01:19:01,356
up after loading the view
typically from a nib.

1864
01:19:01,656 --> 01:19:04,176
So this just means especially
if your interface came

1865
01:19:04,176 --> 01:19:06,206
from a serialized XML file.

1866
01:19:06,416 --> 01:19:09,616
So just a nib file that
you created with the drag

1867
01:19:09,616 --> 01:19:11,856
and drop interface we
saw briefly earlier,

1868
01:19:12,506 --> 01:19:15,096
you can from this
method do more stuff once

1869
01:19:15,126 --> 01:19:17,586
that UI has been
reconstructed into your program.

1870
01:19:17,886 --> 01:19:18,526
Now super.

1871
01:19:18,526 --> 01:19:21,506
For those unfamiliar, less
familiar, what's that line

1872
01:19:21,506 --> 01:19:25,676
of code doing above the comment?

1873
01:19:25,796 --> 01:19:25,926
Yeah.

1874
01:19:26,516 --> 01:19:28,976
[ Inaudible Remark ]

1875
01:19:29,476 --> 01:19:32,176
Exactly, that line
of code is calling--

1876
01:19:32,176 --> 01:19:35,796
passing the viewDidLoad
message to the super class

1877
01:19:36,476 --> 01:19:40,746
which is the parent object
of this particular class.

1878
01:19:40,746 --> 01:19:42,036
Now this too is a convention,

1879
01:19:42,036 --> 01:19:44,186
and if you read the
documentation, Apple will say,

1880
01:19:44,436 --> 01:19:47,796
"if you implement viewDid
Load, you had better remember

1881
01:19:47,796 --> 01:19:49,696
to call our version
of this method

1882
01:19:49,696 --> 01:19:52,276
in case there are some
important stuff that happens

1883
01:19:52,276 --> 01:19:55,476
in this class hierarchy, after
which you can do your thing.

1884
01:19:56,066 --> 01:19:59,626
So I'm fine with
deleting the whole method

1885
01:19:59,946 --> 01:20:03,096
because if I delete it,
it will still exist where?

1886
01:20:04,406 --> 01:20:05,176
In the parent class.

1887
01:20:05,796 --> 01:20:09,806
What I should not do is
ever get into this habit,

1888
01:20:09,946 --> 01:20:13,246
which means my object might
not be properly loaded

1889
01:20:13,456 --> 01:20:16,656
if I have not called the
parent class' implementations

1890
01:20:16,656 --> 01:20:17,886
of the same method.

1891
01:20:18,196 --> 01:20:20,046
So, again, you know that
really by convention

1892
01:20:20,046 --> 01:20:21,946
and from reading the
documentation carefully.

1893
01:20:22,196 --> 01:20:23,876
But viewDidLoad, don't need it.

1894
01:20:24,246 --> 01:20:26,776
DidReceiveMemoryWarning, I'm not
going to need this one either

1895
01:20:26,776 --> 01:20:29,546
so I'll delete, but you can
probably guess what this means.

1896
01:20:29,786 --> 01:20:32,866
If your iPhone has a limited
amount of memory, which it does,

1897
01:20:32,866 --> 01:20:34,516
and you start loading
lots and lots of apps

1898
01:20:34,516 --> 01:20:36,716
and you've gotten a lot of
mail, loaded some big web pages

1899
01:20:36,716 --> 01:20:40,106
in Safari and your phone
is running short on RAM,

1900
01:20:40,686 --> 01:20:44,156
this message might get sent
to your game saying, hey,

1901
01:20:44,606 --> 01:20:47,266
you better calm down and
give up some RAM especially

1902
01:20:47,266 --> 01:20:49,136
if you've been a little
sloppy with your coding

1903
01:20:49,136 --> 01:20:50,806
and you've been allocating
and allocating

1904
01:20:50,806 --> 01:20:52,866
and allocating objects
that you just don't need.

1905
01:20:53,116 --> 01:20:55,916
This is your application's
chance to give memory back

1906
01:20:56,526 --> 01:20:59,546
because if you don't cooperate
and if you don't try to respond

1907
01:20:59,546 --> 01:21:01,476
to this programmatically,
what do you think Apple does

1908
01:21:01,476 --> 01:21:04,386
to your program while
running on the user's phone?

1909
01:21:05,076 --> 01:21:06,726
They just kill it.

1910
01:21:06,726 --> 01:21:09,396
After some number of seconds if
your application has not freed

1911
01:21:09,396 --> 01:21:12,326
up some megabytes per
Apple's request, they kill it

1912
01:21:12,326 --> 01:21:14,486
in the interest of a
good user experience.

1913
01:21:14,486 --> 01:21:17,026
But if you've wondered why
applications might just

1914
01:21:17,026 --> 01:21:18,816
disappear from the
screen, could just be

1915
01:21:18,816 --> 01:21:21,186
that it's really crappy code
and they had some memory error

1916
01:21:21,186 --> 01:21:23,676
and just the thing crashed
and Apple just terminated it

1917
01:21:23,676 --> 01:21:26,436
for that reason, or it
just got memory hungry.

1918
01:21:26,436 --> 01:21:29,566
Didn't adhere to this
admonishment and so,

1919
01:21:29,766 --> 01:21:31,786
iOS killed it for
that reason as well.

1920
01:21:32,516 --> 01:21:36,836
[ Inaudible Remark ]

1921
01:21:37,336 --> 01:21:42,736
Au contraire, so it is very easy
to start allocating objects,

1922
01:21:42,736 --> 01:21:44,556
maybe keeping them
in around an array

1923
01:21:44,556 --> 01:21:47,486
that you actually don't really
need, or even in the context

1924
01:21:47,486 --> 01:21:49,606
of the upcoming project
which we'll talk

1925
01:21:49,606 --> 01:21:50,936
about in the lab on Wednesday.

1926
01:21:51,206 --> 01:21:53,516
You'll have an opportunity
to load tens of thousands

1927
01:21:53,516 --> 01:21:55,696
of English words from
a dictionary file

1928
01:21:55,696 --> 01:21:58,476
that we will provide to you and
it's very easy to keep those

1929
01:21:58,476 --> 01:22:01,336
around in memory in the
original data format and

1930
01:22:01,336 --> 01:22:02,546
yet you don't really
ever need it again.

1931
01:22:02,656 --> 01:22:05,806
And if you don't think about
when you actually need data

1932
01:22:06,226 --> 01:22:08,706
or you don't actually decide,
you know what, I might need it

1933
01:22:08,846 --> 01:22:11,496
but hell I'll just reload
it from the file system

1934
01:22:11,496 --> 01:22:14,076
when I need it, you can start
churning through more memory

1935
01:22:14,226 --> 01:22:15,516
than you actually intend.

1936
01:22:16,146 --> 01:22:19,026
So, it's gotten easier to
deal with memory management,

1937
01:22:19,026 --> 01:22:21,796
thanks to that ARC feature,
automatic reference counting,

1938
01:22:21,796 --> 01:22:23,866
we don't have to worry
about freeing memory as much

1939
01:22:24,186 --> 01:22:26,326
but you can absolutely
allocate more memory

1940
01:22:26,326 --> 01:22:27,856
than you have really,
really need.

1941
01:22:28,056 --> 01:22:28,156
Yeah?

1942
01:22:29,196 --> 01:22:31,196
[ Inaudible Remark ]

1943
01:22:31,376 --> 01:22:32,966
Statue of limitations
in what sense?

1944
01:22:33,516 --> 01:22:37,856
[ Inaudible Remark ]

1945
01:22:38,356 --> 01:22:39,336
Absolutely.

1946
01:22:39,336 --> 01:22:42,446
So, ARC will free your
memory when it determines

1947
01:22:42,446 --> 01:22:44,606
that you don't need it any more.

1948
01:22:44,886 --> 01:22:48,056
But it is very, very easy
for me to declare a pointer

1949
01:22:48,056 --> 01:22:50,756
in a certain way and tuck
it away in my app delegates

1950
01:22:51,056 --> 01:22:52,736
and just keep it in perpetuity

1951
01:22:52,736 --> 01:22:55,576
and the operating system will
never know if I might need it

1952
01:22:55,846 --> 01:22:57,646
because this reduces
to something on this--

1953
01:22:57,646 --> 01:22:59,846
the halting problem if you're
familiar from the theory class.

1954
01:23:00,066 --> 01:23:01,326
You don't necessarily know if

1955
01:23:01,326 --> 01:23:03,826
and when certain code fragments
you wrote will get called

1956
01:23:03,826 --> 01:23:05,826
and if those code fragments
might eventually touch

1957
01:23:05,826 --> 01:23:08,366
that object, it can't
necessarily be freed.

1958
01:23:08,366 --> 01:23:10,646
So, you can very easily
contrive scenarios

1959
01:23:10,646 --> 01:23:12,386
where you consume
way too much memory.

1960
01:23:12,496 --> 01:23:14,526
And as an aside, you
can simulate this.

1961
01:23:14,526 --> 01:23:16,566
Thankfully, you don't have
to just cross your fingers

1962
01:23:16,566 --> 01:23:17,796
when developing iOS apps

1963
01:23:17,796 --> 01:23:20,676
that you'll see this problem
before you ship your program.

1964
01:23:20,966 --> 01:23:24,376
You can go up to hardware
and simulate memory warning

1965
01:23:24,506 --> 01:23:27,116
and you can force the
simulator to send that message

1966
01:23:27,116 --> 01:23:28,686
to your phone so you can figure

1967
01:23:28,686 --> 01:23:31,616
out in advance just how well
you're handling the situation.

1968
01:23:31,896 --> 01:23:33,316
I see another hand went up.

1969
01:23:33,316 --> 01:23:33,436
Yes?

1970
01:23:34,516 --> 01:23:43,676
[ Inaudible Remark ]

1971
01:23:44,176 --> 01:23:45,376
Good question.

1972
01:23:45,376 --> 01:23:47,606
So, whenever in doubts,
let me hold option.

1973
01:23:47,606 --> 01:23:50,126
Let me go ahead and
click on the method name

1974
01:23:50,126 --> 01:23:52,916
and here we have returns a newly
initialized view controller

1975
01:23:52,916 --> 01:23:54,886
with the nib file in
a specified bundle.

1976
01:23:55,106 --> 01:23:57,896
So, if I keep reading here
if you specify nil for--

1977
01:23:57,896 --> 01:23:59,906
let's see, where is the nib.

1978
01:24:00,236 --> 01:24:02,276
So, if we read long
enough we'll get

1979
01:24:02,276 --> 01:24:03,406
to the discussion of the bundle.

1980
01:24:03,646 --> 01:24:06,036
The bundle you can think
of as a program's folder.

1981
01:24:06,036 --> 01:24:07,956
You can have nibs
in different bundles

1982
01:24:07,956 --> 01:24:10,326
and different folders
effectively which can be helpful

1983
01:24:10,326 --> 01:24:14,186
for things like iPad UIs
versus iPhone UIs and the like.

1984
01:24:14,436 --> 01:24:16,566
Nil in this case just
means get me the default

1985
01:24:16,566 --> 01:24:19,306
which is right there and
can be found by its name.

1986
01:24:20,456 --> 01:24:21,186
Good question.

1987
01:24:21,636 --> 01:24:23,406
Again, how to do
with localization too

1988
01:24:23,406 --> 01:24:25,146
if you've got an English
version, Spanish version,

1989
01:24:25,146 --> 01:24:27,316
both of which you created with
the drag and drop interface.

1990
01:24:27,826 --> 01:24:28,106
Yes?

1991
01:24:29,516 --> 01:24:34,096
[ Inaudible Remark ]

1992
01:24:34,596 --> 01:24:37,736
Yes because you could let--

1993
01:24:38,016 --> 01:24:40,016
[ Inaudible Remark ]

1994
01:24:40,016 --> 01:24:42,926
Actually that's a
super good question.

1995
01:24:43,516 --> 01:24:46,116
[ Inaudible Remark ]

1996
01:24:46,616 --> 01:24:49,026
You can definitely do the
latter, implementing a method

1997
01:24:49,026 --> 01:24:50,356
that does that in
the parent class.

1998
01:24:50,356 --> 01:24:53,056
I don't think you can access
the grandparent class directly.

1999
01:24:53,396 --> 01:24:56,646
You need to have some kind
of preparation in advance

2000
01:24:56,646 --> 01:24:58,256
with that parent class.

2001
01:24:58,826 --> 01:25:00,326
Good question.

2002
01:25:00,376 --> 01:25:00,816
All right.

2003
01:25:01,206 --> 01:25:04,286
So, wonderfully interesting
maybe but at the end

2004
01:25:04,286 --> 01:25:06,726
of the day we still made a
pretty crappy application

2005
01:25:06,726 --> 01:25:07,666
that just does this.

2006
01:25:08,096 --> 01:25:11,176
But still, there is some
kind of communication now

2007
01:25:11,176 --> 01:25:15,196
between that nib and my
code and we saw that by way

2008
01:25:15,196 --> 01:25:16,806
of the loading of the nib.

2009
01:25:17,016 --> 01:25:19,926
But this is a purely
unidirectional program.

2010
01:25:20,196 --> 01:25:22,756
When I run it, all I
see is text before me.

2011
01:25:22,756 --> 01:25:24,136
There's no means of interaction.

2012
01:25:24,366 --> 01:25:26,476
So, let's start to make
something more interesting,

2013
01:25:26,476 --> 01:25:28,896
a little more from scratch
that allows me to interact

2014
01:25:29,086 --> 01:25:30,426
with the application itself.

2015
01:25:30,426 --> 01:25:32,246
So, I'm going to go
ahead and close that one,

2016
01:25:32,746 --> 01:25:35,576
create a new project, so not
a new file, a new project,

2017
01:25:35,826 --> 01:25:37,086
single view application.

2018
01:25:37,176 --> 01:25:39,116
And this is generally where
I'm going to start now

2019
01:25:39,176 --> 01:25:41,596
because with single view
application as you saw,

2020
01:25:41,596 --> 01:25:45,186
we get a view controller and
the nib file and the header file

2021
01:25:45,286 --> 01:25:47,166
and we get some additional
wiring in the code,

2022
01:25:47,266 --> 01:25:49,676
but I could absolutely
have recreated

2023
01:25:49,856 --> 01:25:52,746
that simple little hello
class program by starting

2024
01:25:52,746 --> 01:25:55,756
with an empty application
and going to the file menu

2025
01:25:55,756 --> 01:25:59,486
and saying new file and
gradually adding back all

2026
01:25:59,486 --> 01:26:00,636
of the individual files.

2027
01:26:00,946 --> 01:26:02,526
The headache that arises quickly

2028
01:26:02,526 --> 01:26:05,276
if you start doing too many
things from scratch is you have

2029
01:26:05,276 --> 01:26:07,446
to reconfigure some of
those lower level settings

2030
01:26:07,446 --> 01:26:09,206
that you just get for free
with some of the template.

2031
01:26:09,206 --> 01:26:12,396
So, I would start initially
with the more useful templates

2032
01:26:12,556 --> 01:26:15,176
and only once you get
comfortable, try recreating some

2033
01:26:15,176 --> 01:26:17,976
of these things as we may do
in lab this week from scratch

2034
01:26:17,976 --> 01:26:19,316
and reconstructing with someone

2035
01:26:19,316 --> 01:26:20,996
at Apple who's already
done for us.

2036
01:26:21,306 --> 01:26:23,416
So, single view application,
I'm going to go ahead

2037
01:26:23,416 --> 01:26:27,026
and call this thing Nib 1, just
my first example involving a nib

2038
01:26:27,026 --> 01:26:28,696
that I'm going to really
give some thought to.

2039
01:26:28,696 --> 01:26:31,286
I'm going to turn off
storyboards just for now

2040
01:26:31,286 --> 01:26:33,496
to keep things consistent
with that last story.

2041
01:26:33,876 --> 01:26:36,336
Click next, save it wherever

2042
01:26:36,656 --> 01:26:39,496
and now I have essentially the
same exact application I had a

2043
01:26:39,496 --> 01:26:40,226
moment ago.

2044
01:26:40,416 --> 01:26:43,236
Sure enough, if I click on
my nib on the left hand side,

2045
01:26:43,506 --> 01:26:46,056
I just get an empty
iPhone window there.

2046
01:26:46,436 --> 01:26:48,446
So now let's do something
a little more interesting.

2047
01:26:48,446 --> 01:26:50,716
On the right hand side
here, there's a whole bunch

2048
01:26:50,716 --> 01:26:54,416
of Photoshop like options
whereby you can start tinkering

2049
01:26:54,416 --> 01:26:57,786
with the WYSIWYG, what you
see is what you get interface,

2050
01:26:57,856 --> 01:26:59,126
the drag and drop interface.

2051
01:26:59,486 --> 01:27:02,296
And this is frankly a little
overwhelming at first glance

2052
01:27:02,296 --> 01:27:05,156
and I don't think I've clicked
on 50 percent of these over time

2053
01:27:05,156 --> 01:27:07,056
but there's a few that
I occasionally use

2054
01:27:07,136 --> 01:27:08,856
if you're actually
using interface builder

2055
01:27:08,856 --> 01:27:09,516
to create your UI.

2056
01:27:09,516 --> 01:27:13,066
One of them is surely the
objects submenu down here

2057
01:27:13,066 --> 01:27:14,806
where I can drag and
drop things like labels.

2058
01:27:14,806 --> 01:27:15,746
I did that before.

2059
01:27:16,146 --> 01:27:17,036
So, let's start there.

2060
01:27:17,036 --> 01:27:19,356
Let me scroll down and
let me get a text field.

2061
01:27:19,566 --> 01:27:23,236
I want to write an application
I think that allows me to type

2062
01:27:23,236 --> 01:27:26,656
in my name, hit Enter or
click a button on my screen

2063
01:27:26,796 --> 01:27:28,336
and be greeted hello David

2064
01:27:28,336 --> 01:27:30,136
or whatever name I
happen to type in.

2065
01:27:30,136 --> 01:27:32,476
So, some dynamism that we
just haven't had to date.

2066
01:27:32,956 --> 01:27:38,706
So, I could of course just
do this label, hello David,

2067
01:27:38,706 --> 01:27:40,986
but as we saw before that is
not really the point here.

2068
01:27:40,986 --> 01:27:43,166
Well, let's actually have a text
field that the user types in,

2069
01:27:43,166 --> 01:27:45,706
let's see a keyboard pop
up and actually interact.

2070
01:27:45,706 --> 01:27:46,526
So, let's delete that.

2071
01:27:46,806 --> 01:27:48,966
And again, I'm clicking and
deleting just like you would

2072
01:27:48,966 --> 01:27:51,286
in Photoshop or GIMP or
whatever graphics program

2073
01:27:51,286 --> 01:27:52,976
that you might have
tried in the past.

2074
01:27:52,976 --> 01:27:56,176
So, give me a text
field and I'm going to--

2075
01:27:56,176 --> 01:27:57,276
notice a few things happen.

2076
01:27:57,276 --> 01:27:59,106
Much like in Photoshop
and other programs,

2077
01:27:59,386 --> 01:28:01,806
you get some suggestions as
to where to put it relative

2078
01:28:01,806 --> 01:28:04,026
to where the glass is and
how thick a human finger is.

2079
01:28:04,026 --> 01:28:04,936
But you can ignore that.

2080
01:28:05,176 --> 01:28:05,996
But I'll just do that.

2081
01:28:05,996 --> 01:28:07,796
I'm going to now
click and expand it.

2082
01:28:07,946 --> 01:28:11,126
So here I am programming,
all right.

2083
01:28:11,126 --> 01:28:12,996
And now, I have this text field.

2084
01:28:13,226 --> 01:28:16,596
And now, notice up top there's
a whole bunch of these icons.

2085
01:28:16,596 --> 01:28:17,856
Frankly after several years,

2086
01:28:17,856 --> 01:28:19,626
I still don't remember
what, what one is what.

2087
01:28:20,046 --> 01:28:22,676
But if you hover over them,
you get little tool tips.

2088
01:28:22,966 --> 01:28:25,606
So, this one here is called
the Identity inspector,

2089
01:28:25,606 --> 01:28:26,666
we'll click on that in a moment.

2090
01:28:26,666 --> 01:28:28,496
This one is the Attributes
inspector.

2091
01:28:28,766 --> 01:28:30,466
This one is the Size inspector

2092
01:28:30,466 --> 01:28:32,516
and this one is the
Connections inspector.

2093
01:28:32,516 --> 01:28:35,466
So, let's start with
Attributes inspector.

2094
01:28:35,876 --> 01:28:37,246
So, that's this one here.

2095
01:28:37,486 --> 01:28:39,116
And notice I can
do a few things.

2096
01:28:39,116 --> 01:28:41,706
I can change the color of this
text field, the font size,

2097
01:28:41,946 --> 01:28:43,386
how about some placeholder text?

2098
01:28:43,386 --> 01:28:46,286
Just like in HTML 5, you
have a placeholder attribute.

2099
01:28:46,576 --> 01:28:47,946
I'm going to put name here.

2100
01:28:48,206 --> 01:28:49,986
And now notice if
I look at my GUI,

2101
01:28:49,986 --> 01:28:51,766
I have a great outward name.

2102
01:28:51,886 --> 01:28:53,796
So, that's nice, it's a
nice little enhancement.

2103
01:28:53,986 --> 01:28:54,916
How about the background?

2104
01:28:54,916 --> 01:28:57,626
If I click on the gray
background, notice over here

2105
01:28:57,626 --> 01:29:00,646
on the Attributes inspector, I
can choose a different color,

2106
01:29:00,646 --> 01:29:01,826
so that's kind of interesting.

2107
01:29:01,826 --> 01:29:04,476
Let's click other, zoom
out so, I can see it,

2108
01:29:05,106 --> 01:29:07,956
choose the little crayon things
and let's change it to white.

2109
01:29:08,426 --> 01:29:11,676
So, now I have UI more like
the one I got earlier for free

2110
01:29:11,676 --> 01:29:13,516
by just using the
empty application

2111
01:29:13,836 --> 01:29:16,656
and now I need a button to
submit so, where is that?

2112
01:29:17,216 --> 01:29:20,986
I'm going to drag and
drop the button here.

2113
01:29:21,166 --> 01:29:22,566
That's where Apple
recommends I put it.

2114
01:29:22,566 --> 01:29:23,656
I'm going to double click on it

2115
01:29:23,656 --> 01:29:25,656
like in Photoshop
and click type go.

2116
01:29:25,966 --> 01:29:27,966
But notice I could also be
a little more anal and go

2117
01:29:27,966 --> 01:29:30,476
over here and I could
manually type that here.

2118
01:29:30,716 --> 01:29:33,206
I can change the
style of this button.

2119
01:29:33,206 --> 01:29:35,306
Let's see, instead of
a rounded rect button,

2120
01:29:35,596 --> 01:29:37,686
I could do something
like an info button

2121
01:29:37,686 --> 01:29:39,076
but that's not what I wanted.

2122
01:29:39,076 --> 01:29:41,216
That's that little
I icon that you see

2123
01:29:41,216 --> 01:29:42,916
in I think the stock
application.

2124
01:29:43,246 --> 01:29:44,246
So, let's undo that.

2125
01:29:44,406 --> 01:29:45,946
Long story short, I
could start tinkering

2126
01:29:45,946 --> 01:29:47,336
and change the aesthetics
of this.

2127
01:29:47,336 --> 01:29:49,536
But functionally, that just
will get boring quickly.

2128
01:29:49,536 --> 01:29:53,236
So, let me wave my hand at the
remainder of the attributes

2129
01:29:53,286 --> 01:29:55,326
that you can tinker with but
it's the kind of obvious thing,

2130
01:29:55,326 --> 01:29:57,016
size and weight and
other things.

2131
01:29:57,436 --> 01:29:59,246
So, what else might
I want to do here?

2132
01:29:59,246 --> 01:30:03,956
Let me go up to the Identity
inspector which is this icon

2133
01:30:03,956 --> 01:30:05,726
to the left of the
Attributes inspector.

2134
01:30:05,906 --> 01:30:07,486
And I'm not going to
change anything here.

2135
01:30:07,856 --> 01:30:10,826
But notice that there's a
hint of the relationship

2136
01:30:10,826 --> 01:30:12,866
between this drag and
drop interface and code.

2137
01:30:13,376 --> 01:30:16,026
I've highlighted the text field
that I dragged and dropped.

2138
01:30:16,276 --> 01:30:18,936
I've gone to the Identity
inspector and Xcode is hinting

2139
01:30:18,936 --> 01:30:23,216
to me that the type of object
I have effectively instantiated

2140
01:30:23,426 --> 01:30:25,866
or alloced by dragging
and dropping is what?

2141
01:30:27,206 --> 01:30:28,976
It's apparently a UITextField.

2142
01:30:29,266 --> 01:30:31,966
So we'll see in a bit I could
actually write lines of code

2143
01:30:31,966 --> 01:30:35,556
that alloc a UITextField
and encode tell it to go

2144
01:30:35,556 --> 01:30:38,116
to 10 pixels from the top,
20 pixels from the left.

2145
01:30:38,116 --> 01:30:40,086
And I could actually do all
of this programmatically.

2146
01:30:40,296 --> 01:30:43,936
Xcode's purpose in life is
just to simplify this process

2147
01:30:43,936 --> 01:30:47,056
at least for newbies early on
to help get you started quickly

2148
01:30:47,056 --> 01:30:48,556
with the more user friendly UI.

2149
01:30:49,276 --> 01:30:53,356
So unfortunately I've made
my UI simple though it is,

2150
01:30:53,906 --> 01:30:56,386
but it doesn't actually do much.

2151
01:30:56,386 --> 01:30:57,456
Let's see what it does do.

2152
01:30:57,456 --> 01:30:59,326
Let me go ahead and
hit the play button.

2153
01:30:59,816 --> 01:31:01,976
That will spawn the simulator
assuming I've made no

2154
01:31:01,976 --> 01:31:02,776
syntax errors.

2155
01:31:03,086 --> 01:31:04,966
Pretty good so far,
looks like I expected.

2156
01:31:04,966 --> 01:31:06,196
I'm going to click on name.

2157
01:31:06,686 --> 01:31:08,406
And notice I get the
keyboard for free

2158
01:31:08,406 --> 01:31:09,826
because it's a text field.

2159
01:31:09,826 --> 01:31:12,276
Apple knows that when you
get focused to it by clicking

2160
01:31:12,276 --> 01:31:13,866
or touching on it if they
were an actual phone,

2161
01:31:14,186 --> 01:31:15,266
they pop up the keyboard.

2162
01:31:15,546 --> 01:31:19,496
I can start saying
something like D-A-V-I-D.

2163
01:31:19,666 --> 01:31:21,576
Notice it's going to try
to do autocorrect for me.

2164
01:31:21,576 --> 01:31:22,906
So, that's kind of
a nice feature.

2165
01:31:23,166 --> 01:31:27,046
And now I click go and of
course nothing happens, right?

2166
01:31:27,046 --> 01:31:28,096
'Cause what should happen?

2167
01:31:28,096 --> 01:31:30,376
We haven't told the
program what to do.

2168
01:31:30,716 --> 01:31:34,136
So we have to somehow
now associate that button

2169
01:31:34,136 --> 01:31:37,366
and that text field
with actual code.

2170
01:31:37,806 --> 01:31:39,036
So where might we do this?

2171
01:31:39,456 --> 01:31:42,616
So henceforth, even
though you could put code

2172
01:31:42,616 --> 01:31:45,196
in the AppDelegate-- and in
fact let me just clean this up

2173
01:31:45,196 --> 01:31:46,906
and get rid of all
superfluous stuff.

2174
01:31:47,506 --> 01:31:49,696
Even though you could put
code in the AppDelegate

2175
01:31:49,696 --> 01:31:53,016
in the header file or the M
file, you typically won't.

2176
01:31:53,326 --> 01:31:55,456
You'll typically
immediately hand off control

2177
01:31:55,456 --> 01:31:57,536
from the AppDelegate
to a view controller,

2178
01:31:57,536 --> 01:32:00,666
a rootViewController, and
isolate your code there.

2179
01:32:00,816 --> 01:32:03,026
There are some cases where
that's not going to be the case.

2180
01:32:03,026 --> 01:32:07,606
But for now think of a
breakable rule of thumb

2181
01:32:07,606 --> 01:32:09,386
that you should start
writing all of your code

2182
01:32:09,496 --> 01:32:12,276
in the view controller and
not start embedding stuff

2183
01:32:12,276 --> 01:32:13,476
in the AppDelegate.

2184
01:32:14,096 --> 01:32:18,386
Partly by convention here
per the MVC paradigm we just

2185
01:32:18,496 --> 01:32:19,326
mentioned earlier.

2186
01:32:19,806 --> 01:32:21,826
So what should I put where?

2187
01:32:21,826 --> 01:32:23,906
So, if I'm going to put
almost all of my code

2188
01:32:23,906 --> 01:32:27,266
in the view controller class,
that means it should go

2189
01:32:27,266 --> 01:32:29,726
in the H file and in the M file.

2190
01:32:30,456 --> 01:32:33,886
But currently these files
look like this in the H file

2191
01:32:34,416 --> 01:32:37,696
and in the M file,
which looks like this.

2192
01:32:37,696 --> 01:32:40,566
And honestly, this is
really uninteresting.

2193
01:32:40,846 --> 01:32:41,886
I'm going to get rid of this.

2194
01:32:41,886 --> 01:32:43,826
I'm going to get rid
of the private catego--

2195
01:32:43,826 --> 01:32:45,276
the semiprivate category.

2196
01:32:45,536 --> 01:32:47,536
I mean, this class
literally does nothing.

2197
01:32:47,896 --> 01:32:50,236
But I need a way of
somehow linking this class

2198
01:32:50,536 --> 01:32:52,276
with that user interface.

2199
01:32:52,586 --> 01:32:55,566
I need some kinds of
like extension cord

2200
01:32:55,566 --> 01:32:59,326
that wires the nib,
the UI, with my code.

2201
01:32:59,776 --> 01:33:02,626
And so the mechanism
that Apple offers

2202
01:33:02,626 --> 01:33:05,396
for this is something
called an IBOutlet

2203
01:33:06,296 --> 01:33:08,156
for Interface Builder outlet.

2204
01:33:08,156 --> 01:33:10,426
And you can kind of
think of it as a socket

2205
01:33:10,666 --> 01:33:12,266
into which you can plug a cable

2206
01:33:12,266 --> 01:33:14,386
to somehow wire these
two things together.

2207
01:33:14,656 --> 01:33:17,486
So the paradigm for doing
that essentially boils

2208
01:33:17,486 --> 01:33:19,126
down to declaring properties.

2209
01:33:19,556 --> 01:33:23,856
So what I'm going to do in my
ViewController.h file inside

2210
01:33:24,516 --> 01:33:28,136
of my class declaration
here, I'm going to go ahead

2211
01:33:28,136 --> 01:33:31,016
and start typing property
and I'm going to know

2212
01:33:31,016 --> 01:33:33,226
in advance it's going to
be non-atomic, strong.

2213
01:33:33,226 --> 01:33:35,106
In the future we'll
think harder about that.

2214
01:33:35,106 --> 01:33:36,896
But for now let's just
go on faith and say

2215
01:33:36,896 --> 01:33:40,016
that it's no-atomic and
strong or strong, non-atomic.

2216
01:33:40,016 --> 01:33:41,086
Order does not matter.

2217
01:33:41,676 --> 01:33:45,996
IBOutlet and then UITextField.

2218
01:33:46,636 --> 01:33:49,266
And let me close
this window over here

2219
01:33:49,266 --> 01:33:50,246
to give me some more room.

2220
01:33:50,686 --> 01:33:54,176
Star textField semicolon.

2221
01:33:54,836 --> 01:33:58,926
All right, does this look
like a valid declaration?

2222
01:33:59,366 --> 01:34:01,366
[ Inaudible Remark ]

2223
01:34:01,716 --> 01:34:04,116
Exactly. So, this is a bad sign.

2224
01:34:04,116 --> 01:34:06,936
If I click at that
expected semicolon and edge

2225
01:34:06,936 --> 01:34:08,026
and declaration list,

2226
01:34:08,186 --> 01:34:11,426
it actually doesn't quite know
what I'm talking about here.

2227
01:34:11,716 --> 01:34:12,886
Let's try to fix that.

2228
01:34:12,886 --> 01:34:15,306
IBOutlet, resave,
problem goes away.

2229
01:34:15,306 --> 01:34:17,316
When in doubt, simply
changing things

2230
01:34:17,316 --> 01:34:19,486
in Xcode doesn't necessarily
mean Xcode will notice,

2231
01:34:19,486 --> 01:34:22,976
but generally saving the file
will mean it will reanalyze your

2232
01:34:22,976 --> 01:34:25,436
code and get rid of
any warnings or errors.

2233
01:34:25,706 --> 01:34:27,876
OK, so what does this mean?

2234
01:34:27,876 --> 01:34:29,366
Well, a property is just a means

2235
01:34:29,366 --> 01:34:31,706
by which we can synthesize
a getter and a setter

2236
01:34:31,796 --> 01:34:34,836
and give us dot notation access
to some instance variable.

2237
01:34:35,106 --> 01:34:37,826
By convention, what is the
instance variable going

2238
01:34:37,826 --> 01:34:41,746
to be called that is behind
the scenes for this property?

2239
01:34:43,096 --> 01:34:45,046
Any one recall or know?

2240
01:34:46,456 --> 01:34:49,216
This used to be more explicit
but more recent versions

2241
01:34:49,216 --> 01:34:51,246
of iOS just kind
of do this for us.

2242
01:34:51,666 --> 01:34:52,966
By convention, what Apple--

2243
01:34:53,211 --> 01:34:55,211
[ Inaudible Remark ]

2244
01:34:55,406 --> 01:34:56,786
What's underscore text field.

2245
01:34:56,786 --> 01:34:59,846
Yes. So by default what
Apple does these days is

2246
01:34:59,846 --> 01:35:01,096
if you declare a--

2247
01:35:01,096 --> 01:35:06,876
if you declare a property called
textField, they will create

2248
01:35:06,876 --> 01:35:11,996
for you a UITextField star
underscore textField instance

2249
01:35:11,996 --> 01:35:13,876
variable for you
inside of the object.

2250
01:35:13,956 --> 01:35:15,986
But we shouldn't have to
know or care about that

2251
01:35:15,986 --> 01:35:18,166
because the property
gives us getter

2252
01:35:18,166 --> 01:35:20,076
and setter access
to it, but just FYI.

2253
01:35:20,746 --> 01:35:22,876
OK, so what is an IBOutlet?

2254
01:35:22,876 --> 01:35:25,386
Well, what's interesting is
because I typed IBOutlet,

2255
01:35:25,386 --> 01:35:27,476
notice this little
circle that appeared here.

2256
01:35:27,666 --> 01:35:30,606
Let me get rid of IBOutlet,
hit save, it goes away.

2257
01:35:31,036 --> 01:35:34,696
Let me put it back, save,
and come on, comes back.

2258
01:35:35,296 --> 01:35:37,226
So notice, this is kind of cute.

2259
01:35:37,426 --> 01:35:38,856
If I-- can I do this here?

2260
01:35:38,926 --> 01:35:45,136
Damn it, I need to
have two windows open.

2261
01:35:45,136 --> 01:35:46,146
We'll come back to
why that's cute.

2262
01:35:46,486 --> 01:35:46,826
All right.

2263
01:35:46,916 --> 01:35:48,906
So, how do I actually
now connect,

2264
01:35:48,906 --> 01:35:50,906
and that represents
the source of socket

2265
01:35:50,906 --> 01:35:51,866
that I alluded to earlier.

2266
01:35:51,866 --> 01:35:56,286
How do we actually connect that
line of code to my nib file?

2267
01:35:56,516 --> 01:35:58,836
So we can do this in a couple
of ways, but what I'm going

2268
01:35:58,836 --> 01:36:03,736
to do is first, let's say,
go to my nib file over here

2269
01:36:03,736 --> 01:36:06,046
and I'm going to
expand this window here.

2270
01:36:06,266 --> 01:36:07,986
And this is a little
complex at first,

2271
01:36:08,216 --> 01:36:10,246
but now I'm in
ViewController.nib

2272
01:36:11,006 --> 01:36:13,466
which gives me my user interface
that we defined earlier.

2273
01:36:14,016 --> 01:36:15,666
And now notice on
the left hand side,

2274
01:36:15,666 --> 01:36:17,976
we get this little cheat
sheet of placeholders

2275
01:36:18,146 --> 01:36:21,086
where I have the
objects in my view.

2276
01:36:21,546 --> 01:36:22,436
Now what does this mean?

2277
01:36:22,436 --> 01:36:23,676
For now notice that.

2278
01:36:24,226 --> 01:36:26,676
I'm going to just drag this over
here so they're side by side.

2279
01:36:27,226 --> 01:36:30,746
This big rectangle
is itself a UIView.

2280
01:36:30,956 --> 01:36:31,936
Where did it come from?

2281
01:36:31,936 --> 01:36:33,056
It just came with a template.

2282
01:36:33,056 --> 01:36:35,636
I got a default UIView
rectangle by default.

2283
01:36:36,076 --> 01:36:38,466
I then started dragging and
dropping stuff on there.

2284
01:36:38,466 --> 01:36:40,616
Case in point, I'm
going to delete the go

2285
01:36:40,616 --> 01:36:43,816
and notice the placeholder at
top left is about to disappear.

2286
01:36:44,626 --> 01:36:46,236
So that's where it came from.

2287
01:36:46,236 --> 01:36:48,946
So if I go back to my little
menu on the right hand side

2288
01:36:49,246 --> 01:36:51,846
and I grab myself another
rounded rectangular button,

2289
01:36:51,846 --> 01:36:55,016
drag it here, notice it
reappears under view.

2290
01:36:55,176 --> 01:36:57,536
And that's because this
is indeed something akin

2291
01:36:57,536 --> 01:36:58,446
to a canvas.

2292
01:36:58,616 --> 01:37:00,466
They gave me a blank
canvas at first.

2293
01:37:00,466 --> 01:37:01,806
It was gray, then
I made it white.

2294
01:37:01,806 --> 01:37:05,286
And then I started dragging and
dropping different like layers

2295
01:37:05,286 --> 01:37:06,586
or objects on top of it,

2296
01:37:06,846 --> 01:37:09,116
and that's the hierarchal
representation of that.

2297
01:37:09,326 --> 01:37:11,146
Constraints, we'll wave
our hands up for now.

2298
01:37:11,146 --> 01:37:13,186
That just has to do
with supporting devices

2299
01:37:13,186 --> 01:37:14,846
of different sizes like
an iPhone like this,

2300
01:37:14,846 --> 01:37:17,066
an iPhone like this or
like this, or an iPad.

2301
01:37:17,336 --> 01:37:20,036
But those are given to me by
default just to help me out.

2302
01:37:20,516 --> 01:37:23,686
And now up here is
something called file's owner

2303
01:37:23,686 --> 01:37:24,936
and first responder.

2304
01:37:24,936 --> 01:37:26,576
Well, ignore first
responder for now.

2305
01:37:26,876 --> 01:37:29,606
But file's owner
refers to the class

2306
01:37:30,386 --> 01:37:32,886
that has ownership of this nib.

2307
01:37:34,276 --> 01:37:38,216
So per our discussion earlier,
which class owns this nib

2308
01:37:38,836 --> 01:37:41,406
or uses this nib
to load its views?

2309
01:37:41,936 --> 01:37:44,036
The view controller.

2310
01:37:44,336 --> 01:37:46,596
So really, that icon which is

2311
01:37:46,596 --> 01:37:51,396
like a yellow cube is just
an iconic representation

2312
01:37:51,396 --> 01:37:53,726
of my class, it just
represents my code.

2313
01:37:54,086 --> 01:37:55,366
Now why is this useful?

2314
01:37:55,596 --> 01:37:58,616
Well, notice, if I
click on control here

2315
01:37:59,016 --> 01:38:02,236
and then start dragging,
notice that this little--

2316
01:38:02,236 --> 01:38:04,306
we'll call it an extension
cord, starts to appear.

2317
01:38:04,306 --> 01:38:07,326
This blue line starts to
appear and it wants to lock

2318
01:38:07,326 --> 01:38:08,776
on to things over here.

2319
01:38:09,076 --> 01:38:11,196
And I'm going to go ahead
and hover over the textField

2320
01:38:11,576 --> 01:38:13,206
and I'm going to let go.

2321
01:38:13,376 --> 01:38:17,126
And now notice Xcode tells
me that I have a few outlets

2322
01:38:17,186 --> 01:38:19,146
that I can plug this
blue capable into.

2323
01:38:19,506 --> 01:38:21,996
One of which is called
textField,

2324
01:38:22,226 --> 01:38:23,476
one of which is called view.

2325
01:38:23,736 --> 01:38:25,536
The fact that there's a
little minus sign there means

2326
01:38:25,536 --> 01:38:27,436
something's already plugged
in there, just don't touch it,

2327
01:38:27,586 --> 01:38:30,216
that's what came with by default
when we got this whole view.

2328
01:38:30,616 --> 01:38:32,216
But textField looks
familiar, right,

2329
01:38:32,696 --> 01:38:33,906
that was the name
I gave to what?

2330
01:38:34,446 --> 01:38:38,596
That was the name I
gave to the property

2331
01:38:38,716 --> 01:38:40,256
that I typed out in the H file.

2332
01:38:40,556 --> 01:38:44,306
So simply by selecting,
with my mouse, textField,

2333
01:38:44,986 --> 01:38:48,826
I now get some kind of
association, a line goes way.

2334
01:38:49,056 --> 01:38:51,056
But there some kind
of association made

2335
01:38:51,176 --> 01:38:55,726
between the files owner,
aka ViewController.m or .h

2336
01:38:56,086 --> 01:38:59,646
and that particular
widget inside of my view,

2337
01:38:59,646 --> 01:39:01,446
it's like I've plugged
one into the other.

2338
01:39:01,746 --> 01:39:03,136
So what does this really mean?

2339
01:39:03,596 --> 01:39:05,806
This means that when
I run this program,

2340
01:39:06,506 --> 01:39:09,846
my property called textField
which is of what type?

2341
01:39:10,086 --> 01:39:12,426
It's a pointer, which means
it's meant to store an address.

2342
01:39:13,376 --> 01:39:15,566
So what I have just
done with this drag

2343
01:39:15,566 --> 01:39:18,536
and drop interface is I've told
Xcode, when you load my program

2344
01:39:18,536 --> 01:39:20,156
and when you load
this big rectangle

2345
01:39:20,156 --> 01:39:21,966
and you start plopping
the textField there

2346
01:39:21,966 --> 01:39:23,286
and you put the Go button there.

2347
01:39:23,556 --> 01:39:26,136
What-- the last thing I need you
to do before you hand control

2348
01:39:26,136 --> 01:39:29,556
to my application is figure
out, what is the address in RAM

2349
01:39:29,756 --> 01:39:32,166
of that textField
that you dropped there

2350
01:39:32,166 --> 01:39:35,306
for m. What is the address of
that button that you drop there

2351
01:39:35,306 --> 01:39:36,526
for me and put the address

2352
01:39:36,526 --> 01:39:38,416
of the former, the
textField, where?

2353
01:39:39,556 --> 01:39:42,696
Inside of the property that
I declared called textField

2354
01:39:42,696 --> 01:39:44,626
which is a 64 bit
chunk of memory

2355
01:39:45,086 --> 01:39:48,026
in which I want the
address of that textField.

2356
01:39:48,306 --> 01:39:51,396
So again, all interface
builder is doing for us is kind

2357
01:39:51,396 --> 01:39:53,436
of simplifying, dumbing
down if you will,

2358
01:39:53,436 --> 01:39:56,296
the process by which I can
create a user interface

2359
01:39:56,856 --> 01:40:01,306
and it is going to do for
me the process of allocing

2360
01:40:01,446 --> 01:40:04,846
that UITextField, figuring
out with the return value

2361
01:40:04,846 --> 01:40:07,756
of alloc is after
initializing it and then putting

2362
01:40:07,756 --> 01:40:11,776
that 64 bit memory
address into my property.

2363
01:40:12,326 --> 01:40:14,406
And that is again what
the dragging and dropping

2364
01:40:14,406 --> 01:40:16,056
and that blue line
have done for us.

2365
01:40:16,056 --> 01:40:17,336
It's saved me the trouble

2366
01:40:17,506 --> 01:40:20,376
of doing the assignment operator
somewhere in code to figure

2367
01:40:20,376 --> 01:40:22,426
out the address of
that UI text filled.

2368
01:40:23,106 --> 01:40:24,686
So, what's the implication
of this?

2369
01:40:24,686 --> 01:40:27,396
Well, the implication is we
can do one other thing it's one

2370
01:40:27,396 --> 01:40:31,116
thing to have a connection from
my code to inteface builde.

2371
01:40:31,206 --> 01:40:32,206
Why is that useful?

2372
01:40:32,366 --> 01:40:36,746
It means I can probably
programmatically ask the user

2373
01:40:36,746 --> 01:40:37,876
interface for what?

2374
01:40:38,336 --> 01:40:44,556
The value, so, if the user type
something in much like in jQuery

2375
01:40:44,556 --> 01:40:48,066
or Java Script you can ask a
download what is its dot value.

2376
01:40:48,326 --> 01:40:50,286
Similarly now in Objective-C,

2377
01:40:50,286 --> 01:40:52,736
can I programmatically
ask what is the value

2378
01:40:52,736 --> 01:40:53,886
of what the human typed

2379
01:40:53,886 --> 01:40:56,226
into that text field
'cause I have a connection

2380
01:40:56,316 --> 01:40:57,796
from my code to the text field.

2381
01:40:57,796 --> 01:41:00,106
But that's not quite enough
to make an application

2382
01:41:00,356 --> 01:41:02,806
that allows me to type in my
name and then see a prompt

2383
01:41:02,986 --> 01:41:06,136
on the screen because in
order to inform the program

2384
01:41:06,136 --> 01:41:10,716
that I have typed a word what's
the human convention to hit

2385
01:41:10,876 --> 01:41:12,786
like enter or hit the go button.

2386
01:41:12,966 --> 01:41:15,506
So, I somehow need a cable going

2387
01:41:15,506 --> 01:41:20,306
from that go button
back to my code.

2388
01:41:20,506 --> 01:41:24,966
So, the opposite of an IBOutlet
is what we'll call an IBAction

2389
01:41:24,966 --> 01:41:27,486
and I declare that in
my H file as follows,

2390
01:41:27,486 --> 01:41:30,936
dash 'cause it's going to be
an instance method, IBAction.

2391
01:41:31,266 --> 01:41:33,326
And now notice Xcode is
trying to be helpful.

2392
01:41:33,326 --> 01:41:34,606
Let me do that again.

2393
01:41:34,606 --> 01:41:37,126
IBAction. Notice that
it's encouraging me

2394
01:41:37,126 --> 01:41:38,746
to give the selector.

2395
01:41:38,746 --> 01:41:40,006
Selector is a fancy way

2396
01:41:40,006 --> 01:41:43,506
of saying method
name colon ID sender.

2397
01:41:43,506 --> 01:41:45,476
So, we'll come back to
ID sender in a moment.

2398
01:41:45,656 --> 01:41:48,146
I'm going to go ahead and
call this something somewhat

2399
01:41:48,146 --> 01:41:49,456
arbitrary, go.

2400
01:41:50,306 --> 01:41:53,526
This is the method I
want the UI to call

2401
01:41:53,526 --> 01:41:55,366
when I click that go button.

2402
01:41:55,606 --> 01:41:59,306
Now, ID sender, this it turns
out is just a convention now.

2403
01:41:59,546 --> 01:42:02,136
IBActions, if you read
the documentation,

2404
01:42:02,366 --> 01:42:06,756
state that when an IBAction
a.k.a. method is called

2405
01:42:07,026 --> 01:42:09,016
as a result of a
user doing something

2406
01:42:09,016 --> 01:42:12,606
with the user interface, that
method called go in this case

2407
01:42:12,606 --> 01:42:13,846
but it could have
been called anything,

2408
01:42:13,846 --> 01:42:19,276
is past a pointer to
what do you think?

2409
01:42:19,446 --> 01:42:22,266
To the object that was
touched on the screen.

2410
01:42:22,476 --> 01:42:24,736
So, it's a way of potentially
having multiple buttons

2411
01:42:24,736 --> 01:42:26,896
for instance all
invoke the same method

2412
01:42:27,106 --> 01:42:28,896
but I can somehow
identify which one

2413
01:42:28,896 --> 01:42:30,716
of them was actually touched,

2414
01:42:30,776 --> 01:42:32,486
and maybe those buttons
have different meanings

2415
01:42:32,486 --> 01:42:32,966
on the screen.

2416
01:42:32,966 --> 01:42:34,666
In this case it's [inaudible]
'cause I just have one.

2417
01:42:34,666 --> 01:42:36,386
But if I had multiple
that would be useful.

2418
01:42:36,736 --> 01:42:37,616
What is an ID?

2419
01:42:38,806 --> 01:42:39,976
It's a data type recall.

2420
01:42:40,236 --> 01:42:40,346
Yeah?

2421
01:42:41,016 --> 01:42:42,556
[ Inaudible Remark ]

2422
01:42:42,556 --> 01:42:45,596
It's not necessarily a
void pointer, it's a--

2423
01:42:45,596 --> 01:42:49,286
it is a pointer that can be nil.

2424
01:42:49,966 --> 01:42:52,566
So, ID is like saying void
star, for those of you

2425
01:42:52,566 --> 01:42:55,186
who knew C or C++ before.

2426
01:42:55,516 --> 01:42:58,756
But it's a pointer
that can also be nil.

2427
01:42:58,936 --> 01:43:01,056
So, this is sort of the
common Objective-C way

2428
01:43:01,056 --> 01:43:03,246
of saying this is a
pointer but it might be nil.

2429
01:43:03,436 --> 01:43:04,886
It might not be a valid value.

2430
01:43:05,756 --> 01:43:07,996
OK, so, I'm done with that.

2431
01:43:08,236 --> 01:43:10,316
Oh and now notice, here
is where it got useful.

2432
01:43:10,316 --> 01:43:12,466
Because I have this circle
now, notice it's filled in.

2433
01:43:12,606 --> 01:43:14,826
That's because I've plugged
something in to the outlet

2434
01:43:14,826 --> 01:43:15,826
with that little blue line.

2435
01:43:16,186 --> 01:43:17,706
All right, so IBAction go.

2436
01:43:17,776 --> 01:43:18,766
All right, I'm really eager now.

2437
01:43:18,766 --> 01:43:20,406
I'm going to go ahead
and click run here.

2438
01:43:21,096 --> 01:43:23,556
All right, so I'm going to
see my UI in the simulator.

2439
01:43:24,126 --> 01:43:31,346
And notice for some
reason the latest version

2440
01:43:31,346 --> 01:43:34,446
of the iOS simulator is slow
to start but here it is now.

2441
01:43:34,446 --> 01:43:38,396
I'm going to go ahead, very
exciting, D-A-V-I-D, go.

2442
01:43:40,896 --> 01:43:42,426
Why is it not working?

2443
01:43:42,806 --> 01:43:44,556
Yeah, I didn't link it, right?

2444
01:43:44,556 --> 01:43:46,896
All I did was declare a method
that happened to be called go

2445
01:43:46,896 --> 01:43:49,796
but I made no programmatic
connection between that method

2446
01:43:49,796 --> 01:43:52,476
and the UI, and indeed the
little cute circle here is still

2447
01:43:52,476 --> 01:43:54,726
empty, we haven't wired
those two things together.

2448
01:43:55,106 --> 01:43:57,826
So, let me go back
to my nib file.

2449
01:43:58,336 --> 01:44:00,316
And actually notice,
Xcode is pretty good.

2450
01:44:00,316 --> 01:44:03,156
Xcode underneath the hood uses
a compiler called clang whose

2451
01:44:03,156 --> 01:44:05,526
error messages are pretty decent
and pretty human friendly.

2452
01:44:05,906 --> 01:44:08,566
Incomplete implementation,
it's slapping me on the wrist

2453
01:44:08,566 --> 01:44:10,896
for having done exactly
what we identified

2454
01:44:10,896 --> 01:44:11,826
as the problem already.

2455
01:44:12,216 --> 01:44:14,886
So, if you get this here,
notice it took my files away.

2456
01:44:14,886 --> 01:44:15,846
Not a problem.

2457
01:44:15,846 --> 01:44:17,426
See the little folder
icon at top?

2458
01:44:17,726 --> 01:44:19,956
Click that, and I'm back
where I expect to be.

2459
01:44:20,266 --> 01:44:21,556
Let me go to my nib file,

2460
01:44:21,976 --> 01:44:24,926
and now notice this time
I want the arrow to--

2461
01:44:24,926 --> 01:44:26,706
the blue line to go in
the other direction.

2462
01:44:26,706 --> 01:44:29,096
I want the button to
be linked to my code

2463
01:44:29,096 --> 01:44:32,016
so that messages can be
sent from button to code.

2464
01:44:32,276 --> 01:44:33,766
So, again, I'm going
to hold control.

2465
01:44:33,766 --> 01:44:35,506
I'm going to click on go.

2466
01:44:36,006 --> 01:44:39,206
And now notice, little blue
line starts to form and it wants

2467
01:44:39,206 --> 01:44:42,766
to cling to a few things but
I know just intellectually

2468
01:44:42,766 --> 01:44:45,366
that this belongs
tied to my class

2469
01:44:45,716 --> 01:44:47,436
where that IBAction was defined.

2470
01:44:47,696 --> 01:44:51,426
So, if I let go over files
owner, notice that the events

2471
01:44:51,506 --> 01:44:54,506
that I can wire this into is go.

2472
01:44:54,736 --> 01:44:56,766
And here honestly Apple
is just inconsistent.

2473
01:44:56,766 --> 01:44:58,576
They call it an IBAction
in one place.

2474
01:44:58,576 --> 01:44:59,916
Here they call it a sent event.

2475
01:44:59,916 --> 01:45:01,306
So, the same exact thing.

2476
01:45:01,676 --> 01:45:06,386
So, I'm going to click go and
that linkage has now been made.

2477
01:45:06,386 --> 01:45:08,806
Indeed, if I go back to my
code, notice the little--

2478
01:45:08,806 --> 01:45:14,386
whoops, notice in the H file
the little circle has been

2479
01:45:14,386 --> 01:45:15,086
filled in.

2480
01:45:15,256 --> 01:45:16,686
And I can confirm as much now.

2481
01:45:16,686 --> 01:45:20,126
If I go to the nib
file and I click on--

2482
01:45:20,366 --> 01:45:24,106
control click on this
button, notice that whole--

2483
01:45:24,106 --> 01:45:25,436
it turns out there's more

2484
01:45:25,606 --> 01:45:27,666
than just the default
blue line that can happen.

2485
01:45:27,976 --> 01:45:29,896
Notice there's a
whole bunch of events

2486
01:45:30,366 --> 01:45:33,426
that a little itsy bitsy button
like the go button can send.

2487
01:45:33,726 --> 01:45:35,786
The one that's inferred
to be the default

2488
01:45:35,786 --> 01:45:37,906
when you just start clicking
with control and dragging

2489
01:45:37,906 --> 01:45:41,126
and creating a blue line is an
event called Touch Up Inside.

2490
01:45:41,436 --> 01:45:44,056
That's like OnKeyUp,
it's on TouchUp.

2491
01:45:44,056 --> 01:45:45,506
So as soon as my
finger comes up,

2492
01:45:45,716 --> 01:45:47,386
this event is going
to get fired.

2493
01:45:47,566 --> 01:45:49,146
But clearly there's
other ones related

2494
01:45:49,146 --> 01:45:50,756
to dragging and other things.

2495
01:45:50,756 --> 01:45:52,686
But that's the one that
Xcode does by default.

2496
01:45:52,916 --> 01:45:54,556
Suppose you didn't really
know that or you wanted

2497
01:45:54,556 --> 01:45:56,096
to change it or you just goof.

2498
01:45:56,266 --> 01:45:57,526
Notice I can undo this.

2499
01:45:58,146 --> 01:45:59,456
Oops, that was a mistake.

2500
01:45:59,616 --> 01:46:01,526
But no worries, I can zoom out.

2501
01:46:01,826 --> 01:46:04,926
And now notice, I can
go to Touch Up Inside,

2502
01:46:05,366 --> 01:46:07,006
the little circle
becomes a plus.

2503
01:46:07,926 --> 01:46:12,146
And if I click there and notice,
I can drag to files owner,

2504
01:46:12,476 --> 01:46:15,796
choose go again, so two
different ways to now do this.

2505
01:46:15,796 --> 01:46:17,046
And we'll give you a third.

2506
01:46:17,296 --> 01:46:20,866
If I now delete that, so
the blue line is now gone.

2507
01:46:21,386 --> 01:46:23,616
Now I can enter a
different mode in Xcode

2508
01:46:23,616 --> 01:46:25,676
and the screen is
a little small,

2509
01:46:25,676 --> 01:46:27,076
so this won't quite
do it justice.

2510
01:46:27,076 --> 01:46:28,556
Let me hide the view over here.

2511
01:46:28,766 --> 01:46:30,986
And notice if you just click
around, you can do no wrong,

2512
01:46:30,986 --> 01:46:31,906
you can just hide things

2513
01:46:31,906 --> 01:46:33,816
by clicking somewhat
obvious buttons.

2514
01:46:33,816 --> 01:46:37,236
I'm going to go to this little
butler icon in the middle

2515
01:46:37,616 --> 01:46:39,706
which is called the--
what's he called?

2516
01:46:39,756 --> 01:46:40,966
Assistant editor.

2517
01:46:41,316 --> 01:46:44,876
And if I click this, notice
that I get a split screen.

2518
01:46:44,916 --> 01:46:46,676
Unfortunately my screen
is somewhat small,

2519
01:46:46,676 --> 01:46:47,956
so this is somewhat
underwhelming.

2520
01:46:47,956 --> 01:46:49,966
But if you have a nice monitor
at home, this is compelling.

2521
01:46:50,426 --> 01:46:52,746
Notice now that it's
a butler when--

2522
01:46:52,746 --> 01:46:54,586
a butler icon because
it's the assistant

2523
01:46:54,586 --> 01:46:55,636
and it's trying to be helpful.

2524
01:46:55,636 --> 01:46:58,696
When you click that icon,
Xcode will typically open

2525
01:46:58,696 --> 01:47:00,506
in another window the file

2526
01:47:00,506 --> 01:47:03,726
that it statistically thinks you
are likely to care about next.

2527
01:47:03,986 --> 01:47:05,166
And in this case,
it got it right.

2528
01:47:05,396 --> 01:47:06,726
I'm in my H file.

2529
01:47:07,156 --> 01:47:11,566
I want it to be in my nib
file also because now,

2530
01:47:11,566 --> 01:47:14,656
notice this cute little circles
finally have some applicability

2531
01:47:14,656 --> 01:47:17,016
because I can see them on
the screen at the same time.

2532
01:47:17,316 --> 01:47:20,856
So if I click on control
here and drag down to here--

2533
01:47:21,216 --> 01:47:22,716
oops, sorry, goes
the other direction.

2534
01:47:22,716 --> 01:47:25,866
If I click on the plus there
and go to my button here,

2535
01:47:26,096 --> 01:47:29,096
I can actually wire it to
go in exactly the same way.

2536
01:47:29,096 --> 01:47:31,646
So many, many different ways
to do this, let me go back

2537
01:47:31,646 --> 01:47:32,906
to the simpler code window.

2538
01:47:32,906 --> 01:47:35,946
I personally find it easier to
just control click on the icon

2539
01:47:36,326 --> 01:47:40,266
and then just start dragging
and dropping, let go, choose go.

2540
01:47:40,506 --> 01:47:41,626
Everything is now wired.

2541
01:47:41,626 --> 01:47:44,016
You can verify as much
by clicking control,

2542
01:47:44,286 --> 01:47:47,446
looking that Touch Up Inside
is referencing File's Owner's

2543
01:47:47,446 --> 01:47:48,246
go method.

2544
01:47:48,456 --> 01:47:51,286
And conversely, if I go to
File's Owner, click control,

2545
01:47:51,556 --> 01:47:53,746
zoom in here, there's
some stuff I didn't do.

2546
01:47:53,746 --> 01:47:56,846
Apple did some of this, but
notice that textField is linked

2547
01:47:56,846 --> 01:47:59,626
up to the Text Field Name which
is exactly what's in the UI.

2548
01:48:00,096 --> 01:48:03,026
So again, three different ways
to now wire this thing together.

2549
01:48:03,456 --> 01:48:04,036
All right.

2550
01:48:04,446 --> 01:48:06,526
So now, OK, really excited now,

2551
01:48:06,526 --> 01:48:08,356
I've got everything
wired up bidirectionally.

2552
01:48:08,356 --> 01:48:09,196
I'm going to click run.

2553
01:48:09,536 --> 01:48:10,646
It says stop Nib1.

2554
01:48:10,646 --> 01:48:12,276
That just means the
simulator is already running.

2555
01:48:12,276 --> 01:48:13,736
I'm going to ignore
that and just tell it

2556
01:48:13,736 --> 01:48:16,026
to restart the simulator
with my new code.

2557
01:48:16,396 --> 01:48:20,906
Everything is wired up, very
excited, type in D-A-V-I-D, go.

2558
01:48:20,906 --> 01:48:24,426
Damn it, my first crash.

2559
01:48:24,856 --> 01:48:26,806
So this is the debugging
window down here.

2560
01:48:27,046 --> 01:48:28,066
Anything that looks

2561
01:48:28,066 --> 01:48:30,706
that unintelligible means
you screwed up somewhere.

2562
01:48:31,086 --> 01:48:32,786
So what did I do wrong?

2563
01:48:32,856 --> 01:48:32,923
Yeah?

2564
01:48:33,016 --> 01:48:35,766
>> You didn't implement
the method.

2565
01:48:35,766 --> 01:48:36,916
>> I didn't implement
the method.

2566
01:48:36,916 --> 01:48:40,606
I declared to the world I am
implementing a method called go

2567
01:48:40,606 --> 01:48:42,696
and then I never got
around to implementing it.

2568
01:48:42,696 --> 01:48:45,836
So it's like I've referenced
an invalid pointer effectively.

2569
01:48:46,256 --> 01:48:49,516
So, let me zoom out, where do
I-- oh, my God, what the heck--

2570
01:48:49,666 --> 01:48:51,666
so there is your threads
we promised earlier.

2571
01:48:51,666 --> 01:48:52,356
I'm going to go back

2572
01:48:52,356 --> 01:48:54,836
to the folder view 'cause
I know what the problem is.

2573
01:48:55,106 --> 01:48:56,416
Where do I solve this problem?

2574
01:48:57,266 --> 01:48:59,266
[ Inaudible Remark ]

2575
01:48:59,516 --> 01:49:02,156
Yeah, the dot M file
for my view controller.

2576
01:49:02,366 --> 01:49:04,026
So first, let me
go to my H file.

2577
01:49:04,546 --> 01:49:07,396
Let me go ahead and copy the
signature of this method,

2578
01:49:07,396 --> 01:49:08,956
just to save myself
some key strokes.

2579
01:49:08,956 --> 01:49:12,876
Let me go to ViewController.m
and notice Xcode knew

2580
01:49:12,876 --> 01:49:15,526
and warned me in yellow
incomplete implementation,

2581
01:49:15,696 --> 01:49:18,566
so that's another advantage of
using the H file in promising

2582
01:49:18,566 --> 01:49:19,456
to implement something.

2583
01:49:19,706 --> 01:49:21,176
You can now follow
through on that.

2584
01:49:21,176 --> 01:49:23,906
Let me go ahead and start
implementing that here.

2585
01:49:24,096 --> 01:49:24,526
Question?

2586
01:49:25,516 --> 01:49:35,356
[ Inaudible Remark ]

2587
01:49:35,856 --> 01:49:37,386
That is a good question.

2588
01:49:37,386 --> 01:49:38,126
Let us see.

2589
01:49:38,436 --> 01:49:41,326
Let's click on the arrow,
incomplete implementation.

2590
01:49:42,716 --> 01:49:47,026
Let's see if we go up here,
incomplete implementation,

2591
01:49:47,576 --> 01:49:49,116
method definition, there we go.

2592
01:49:49,216 --> 01:49:51,986
So short answer, yes, and
I found that by clicking

2593
01:49:51,986 --> 01:49:55,106
on the little yield sign
here, the exclamation point,

2594
01:49:55,376 --> 01:49:56,756
digging down through
the hierarchy.

2595
01:49:56,856 --> 01:50:00,096
And then, Xcode reveals some
more explicit information.

2596
01:50:00,736 --> 01:50:00,876
Yes?

2597
01:50:01,516 --> 01:50:04,336
[ Inaudible Remark ]

2598
01:50:04,836 --> 01:50:07,556
Because-- that's
a good question.

2599
01:50:07,556 --> 01:50:12,056
It is not a-- well, so we
could turn on certain levels

2600
01:50:12,056 --> 01:50:14,246
of warnings to tell
the compiler,

2601
01:50:14,246 --> 01:50:15,496
"Don't let me compile this."

2602
01:50:15,776 --> 01:50:18,546
By default, it will assume
that I know what I'm doing.

2603
01:50:18,546 --> 01:50:20,906
Maybe I'm linking in the
implementation of that method

2604
01:50:20,906 --> 01:50:23,266
in one of those libraries
that we saw earlier.

2605
01:50:23,626 --> 01:50:25,306
So maybe buried deep inside one

2606
01:50:25,306 --> 01:50:26,986
of these frameworks
is an implementation

2607
01:50:26,986 --> 01:50:29,246
and I'm just making a commitment
to the linker that it will--

2608
01:50:29,246 --> 01:50:30,666
those bits will appear
eventually.

2609
01:50:31,306 --> 01:50:34,646
Short answer is we could change
that behavior if we wanted to.

2610
01:50:34,696 --> 01:50:36,686
By default it doesn't
protect us from ourselves.

2611
01:50:36,686 --> 01:50:36,776
Yes?

2612
01:50:36,856 --> 01:50:41,766
>> That string in hexadecimal,
is that the location where--

2613
01:50:41,766 --> 01:50:44,456
>> Yes. This is all-- these are
all hex values and presumably,

2614
01:50:44,456 --> 01:50:45,476
I don't know for sure here,

2615
01:50:45,706 --> 01:50:47,876
refer to memory addresses
probably inside

2616
01:50:47,876 --> 01:50:49,616
of this dynamic library
which is code

2617
01:50:49,616 --> 01:50:51,566
that Apple wrote that's
been compiled elsewhere,

2618
01:50:52,636 --> 01:50:53,436
I'm guessing.

2619
01:50:53,546 --> 01:50:53,766
RJ?

2620
01:50:54,516 --> 01:50:56,516
[ Inaudible Remark ]

2621
01:50:57,016 --> 01:51:00,000
[ Laughter ]

2622
01:51:00,016 --> 01:51:01,636
[ Inaudible Remark ]

2623
01:51:01,636 --> 01:51:02,666
There we go.

2624
01:51:02,806 --> 01:51:04,686
So yes, it's a little part--

2625
01:51:04,686 --> 01:51:07,546
hard to parse visually
but terminating app due

2626
01:51:07,546 --> 01:51:08,666
to uncaught exception

2627
01:51:08,666 --> 01:51:10,766
in NSInvalidArgumentException
not helpful,

2628
01:51:10,766 --> 01:51:11,856
but this gets more helpful.

2629
01:51:12,266 --> 01:51:16,196
Go, unrecognized selector
sent to instant such and such.

2630
01:51:16,486 --> 01:51:17,616
So this means exactly that.

2631
01:51:17,616 --> 01:51:21,726
You passed a message to an
object and that message that--

2632
01:51:22,476 --> 01:51:26,036
that implementation was not
actually there even though you

2633
01:51:26,036 --> 01:51:27,786
promised that it would be.

2634
01:51:28,536 --> 01:51:29,196
All right.

2635
01:51:29,826 --> 01:51:31,086
So, easy fix.

2636
01:51:31,386 --> 01:51:34,646
Let's go ahead and in my M
file start implementing this.

2637
01:51:34,646 --> 01:51:36,026
And let's keep this
super simple.

2638
01:51:36,206 --> 01:51:39,426
I just want a sanity check that
I'm doing something right today.

2639
01:51:39,426 --> 01:51:45,916
So, I'm going to call NSLog of
"here" just to prove to myself

2640
01:51:45,916 --> 01:51:47,116
that this code is
getting called.

2641
01:51:47,416 --> 01:51:51,596
But this isn't quite right, so
couple of things wrong here.

2642
01:51:52,796 --> 01:51:55,336
I haven't done something
right here.

2643
01:51:55,966 --> 01:51:58,516
Oh, and I'm also
in the wrong file.

2644
01:51:58,516 --> 01:52:00,386
So, all right, stupid Xcode.

2645
01:52:00,386 --> 01:52:01,566
So even though that's
highlighted,

2646
01:52:01,566 --> 01:52:02,606
I'm not actually there.

2647
01:52:03,036 --> 01:52:05,966
Let me click on the M file and
now put this where it belongs.

2648
01:52:06,916 --> 01:52:07,826
But there's another error.

2649
01:52:08,516 --> 01:52:10,626
[ Inaudible Remark ]

2650
01:52:11,126 --> 01:52:12,576
Yeah, where do I put an at sign.

2651
01:52:12,576 --> 01:52:14,776
>> In front of the
first quotation mark?

2652
01:52:14,776 --> 01:52:16,776
>> OK, in front of the
first quote mark but why?

2653
01:52:16,776 --> 01:52:18,776
That looks like a string
already, it's quoted.

2654
01:52:19,246 --> 01:52:21,246
[ Inaudible Remark ]

2655
01:52:21,476 --> 01:52:23,006
It's not an NSString.

2656
01:52:23,006 --> 01:52:25,586
So an NSString is a
special string object.

2657
01:52:25,586 --> 01:52:28,806
It's a class construct that
means there's more to the string

2658
01:52:28,806 --> 01:52:30,076
than just a sequence
of characters.

2659
01:52:30,076 --> 01:52:31,596
It's not a char star
from the world

2660
01:52:31,596 --> 01:52:34,066
of C. It's an actual
Objective-C object.

2661
01:52:34,276 --> 01:52:37,166
So NSLog, if you want more
information, click on option,

2662
01:52:37,166 --> 01:52:39,176
click the question mark and
you can read more about it.

2663
01:52:39,326 --> 01:52:41,666
But again, it's like printf, but
it prints to a standard place

2664
01:52:41,666 --> 01:52:44,066
and that standard place for
now is going to be down here.

2665
01:52:44,266 --> 01:52:47,926
This is also useful if
you're programming the app

2666
01:52:48,096 --> 01:52:49,146
onto the phone.

2667
01:52:49,146 --> 01:52:50,446
You can actually
connect the phone

2668
01:52:50,446 --> 01:52:52,216
to your computer via USB cable

2669
01:52:52,216 --> 01:52:53,856
and you can actually
see the NSLogs

2670
01:52:54,246 --> 01:52:55,676
of your application running

2671
01:52:55,676 --> 01:52:57,706
on an actual device
via the same mechanism.

2672
01:52:57,946 --> 01:52:59,546
Generally though,
you should remove all

2673
01:52:59,546 --> 01:53:02,116
of these things before you ship
your product otherwise anyone

2674
01:53:02,116 --> 01:53:04,266
can see you're debugging
messages by plugging their phone

2675
01:53:04,266 --> 01:53:07,026
into like iTunes and watching
some of these messages

2676
01:53:07,026 --> 01:53:08,696
or the Xcode counterpart.

2677
01:53:09,156 --> 01:53:11,476
OK, so what I'm promising
here is

2678
01:53:11,476 --> 01:53:14,136
that when the go method is
called, it's just going to print

2679
01:53:14,136 --> 01:53:16,566
to the log at the bottom of
the screen here with a bunch

2680
01:53:16,566 --> 01:53:18,376
of exclamation points,
and that will be progress.

2681
01:53:18,376 --> 01:53:20,576
More so-- more than
we had thus far.

2682
01:53:20,576 --> 01:53:23,846
So let's run this with the
play button down here, OK.

2683
01:53:24,966 --> 01:53:27,716
D-A-V-I-D, go.

2684
01:53:27,906 --> 01:53:31,376
Whoa! Finally, I'm
programming for real.

2685
01:53:31,376 --> 01:53:33,996
So, it's not that interesting.

2686
01:53:33,996 --> 01:53:36,326
The timestamp is changing so
it's happening again and again

2687
01:53:36,326 --> 01:53:39,046
but of course I'm not actually
getting the value that's

2688
01:53:39,046 --> 01:53:39,956
in that text field.

2689
01:53:40,156 --> 01:53:42,096
So we need to go
one step further.

2690
01:53:42,096 --> 01:53:44,836
So how can we about
getting that value?

2691
01:53:45,176 --> 01:53:48,136
Well, let me try--
how best to do this?

2692
01:53:48,176 --> 01:53:48,866
Let's try this.

2693
01:53:49,466 --> 01:53:55,736
So, hello comma, how do I put
a placeholder in for a string

2694
01:53:55,736 --> 01:53:57,326
that I'm going to insert later?

2695
01:53:58,236 --> 01:54:00,406
Yeah, so percent S--

2696
01:54:00,856 --> 01:54:02,206
>> Percent at--

2697
01:54:02,296 --> 01:54:04,836
>> Percent at sign if it's
going to be an NSString,

2698
01:54:04,836 --> 01:54:06,826
so I would have figured that
the hard way if I got it wrong

2699
01:54:06,826 --> 01:54:08,746
and it wouldn't-- and it would
give me a little red flag.

2700
01:54:09,116 --> 01:54:14,486
So hello here, close quote,
comma, now I need a way

2701
01:54:14,636 --> 01:54:16,456
of getting at my name
that's been typed

2702
01:54:16,456 --> 01:54:17,976
in by the human
programmatically.

2703
01:54:18,506 --> 01:54:22,296
So recall that we have in my
dot H file a property called

2704
01:54:22,296 --> 01:54:22,966
text field.

2705
01:54:23,346 --> 01:54:26,456
How do I get access to that
tech-- that property in code?

2706
01:54:26,456 --> 01:54:26,976
What do I type?

2707
01:54:27,016 --> 01:54:28,936
[ Inaudible Remark ]

2708
01:54:28,936 --> 01:54:32,246
Self, yeah, but typically
self.textField

2709
01:54:32,466 --> 01:54:34,496
and notice Xcode knows
about it, so it's going

2710
01:54:34,496 --> 01:54:35,576
to auto complete for me.

2711
01:54:35,576 --> 01:54:36,966
And then how do I get

2712
01:54:36,966 --> 01:54:39,896
at the value 'cause
text field is an object.

2713
01:54:40,506 --> 01:54:42,926
I'm actually not sure so I
could read the documentation

2714
01:54:42,926 --> 01:54:45,476
but I've read it in advance and
if I start autocompleting here,

2715
01:54:45,706 --> 01:54:49,966
it turns out that in N--
that a UITextField object,

2716
01:54:50,266 --> 01:54:52,756
if you read the manual,
has a property associated

2717
01:54:52,756 --> 01:54:59,826
with it called a called text
whose data type is apparently an

2718
01:54:59,876 --> 01:55:00,956
NSString pointer.

2719
01:55:00,956 --> 01:55:01,786
So it's a string.

2720
01:55:01,786 --> 01:55:02,846
So I'm going to go ahead

2721
01:55:02,846 --> 01:55:08,526
and just do self.textField.text
close paren, semicolon, save,

2722
01:55:08,526 --> 01:55:10,186
and all those errors went away.

2723
01:55:10,786 --> 01:55:13,336
So this is a very
poor man's approach

2724
01:55:13,336 --> 01:55:15,916
to making an application
that dynamically says "Hello,

2725
01:55:15,916 --> 01:55:19,526
David" or whoever I type in
albeit to the log down here.

2726
01:55:19,846 --> 01:55:21,416
So let's go ahead
and clear the log

2727
01:55:21,416 --> 01:55:22,856
by clicking this
little Clear button.

2728
01:55:23,126 --> 01:55:25,026
Let's rerun the program
at top left.

2729
01:55:25,716 --> 01:55:26,596
Here's the program.

2730
01:55:26,596 --> 01:55:30,846
I'm going to go ahead and type
in D-A-V-I-D, go, all right.

2731
01:55:31,116 --> 01:55:34,976
So no one else besides me
with a USB cable can see this

2732
01:55:35,256 --> 01:55:37,936
but at least I'm
programmatically accessing the

2733
01:55:37,936 --> 01:55:41,366
name and if I change
this to, let's say ROB,

2734
01:55:42,106 --> 01:55:44,226
notice that now we're
seeing Rob's name

2735
01:55:44,326 --> 01:55:45,316
on the screen as well.

2736
01:55:45,976 --> 01:55:47,596
Question over here?

2737
01:55:47,596 --> 01:55:49,686
All right, but we
can do better, right?

2738
01:55:49,686 --> 01:55:53,216
This is the horrible, horrible
iOS application, so-- or PROB.

2739
01:55:53,216 --> 01:55:55,946
All right, so let's see
how we can do better.

2740
01:55:55,946 --> 01:55:58,956
So let's borrow some of the same
constructs we've been relying

2741
01:55:58,956 --> 01:56:00,186
on thus far in the language

2742
01:56:00,366 --> 01:56:02,316
and do something a
little more interesting.

2743
01:56:02,316 --> 01:56:06,636
I'm going to go ahead and first
let's say declare an NSString

2744
01:56:07,186 --> 01:56:10,706
pointer called S and I'm
looking at my cheat sheet here

2745
01:56:10,706 --> 01:56:13,526
so that we produce now live will
actually match the sample code

2746
01:56:13,526 --> 01:56:14,996
that you can download
and play with later

2747
01:56:14,996 --> 01:56:18,076
so that we get this right, and
I'm going to declare NSString

2748
01:56:18,076 --> 01:56:20,626
and it turns out you
don't always have

2749
01:56:20,626 --> 01:56:22,086
to do alloc and init.

2750
01:56:22,086 --> 01:56:25,306
A lot of classes, NSString among
them, have convenience methods

2751
01:56:25,466 --> 01:56:27,636
that do the alloc
on the init for you.

2752
01:56:27,816 --> 01:56:29,216
And this is one such method,

2753
01:56:29,216 --> 01:56:33,356
stringWithFormat is a message I
can send to the NSString class.

2754
01:56:33,356 --> 01:56:37,206
It's a class method and I can
very quickly get back a string

2755
01:56:37,206 --> 01:56:39,016
formatted in a certain
way and I'm going to do

2756
01:56:39,016 --> 01:56:43,266
"Hello comma percent
at" and then I'm going

2757
01:56:43,416 --> 01:56:48,296
to dynamically plug in
self.textField.text semicolon

2758
01:56:49,206 --> 01:56:51,906
So to recap, what's
going on here?

2759
01:56:51,906 --> 01:56:54,856
On the left hand side of the
equal sign, what is happening?

2760
01:56:55,696 --> 01:56:56,646
Someone? Yeah?

2761
01:56:57,516 --> 01:56:58,556
>> Creating a pointer.

2762
01:56:59,176 --> 01:57:01,196
>> Creating a pointer,
64 bits of memory,

2763
01:57:01,196 --> 01:57:02,946
it's going to be the address
of what kind of object?

2764
01:57:03,736 --> 01:57:05,326
NSString. Am I instantiating
an object?

2765
01:57:06,286 --> 01:57:09,066
So not yet, not until the other
side, the right hand side.

2766
01:57:09,066 --> 01:57:11,426
At which point, I'm apparently
calling a method called

2767
01:57:11,426 --> 01:57:15,666
stringWithFormat that if I read
the documentation will tell me

2768
01:57:15,796 --> 01:57:18,016
that it's a class method
that returns a pointer

2769
01:57:18,016 --> 01:57:20,616
to an NSString object
constructed per

2770
01:57:20,616 --> 01:57:21,776
that format string.

2771
01:57:22,356 --> 01:57:23,826
So, what do I want
to do with this?

2772
01:57:23,826 --> 01:57:26,126
Well, Xcode if I'd click
on the exclamation point,

2773
01:57:26,126 --> 01:57:28,726
it's unused variable S. I've
not done anything interesting.

2774
01:57:29,016 --> 01:57:31,016
So let's do something
a little more sexy.

2775
01:57:31,076 --> 01:57:34,886
UIAlertView, and for those
of you with iOS devices,

2776
01:57:34,886 --> 01:57:35,816
this will look familiar.

2777
01:57:36,156 --> 01:57:37,666
Pointer, I'm going
to call it an alert.

2778
01:57:37,986 --> 01:57:40,846
And now, this is going to look a
little messy until I tidy it up.

2779
01:57:40,846 --> 01:57:46,426
UIAlertView alloc, and now
I'm going to initWith--

2780
01:57:46,426 --> 01:57:47,846
this is one hell of a method.

2781
01:57:47,846 --> 01:57:50,976
All right, so trivia,
what's this method called?

2782
01:57:54,696 --> 01:57:58,756
OK, initWithTitle
message delegate--

2783
01:57:58,756 --> 01:57:59,706
>> cancelButtonTitle.

2784
01:57:59,706 --> 01:58:01,836
>> -- cancelButtonTitle
otherButtonTitles.

2785
01:58:02,506 --> 01:58:04,026
OK, so it doesn't always
read like a sentence

2786
01:58:04,026 --> 01:58:05,916
but that is the name
of the method so it's

2787
01:58:05,916 --> 01:58:07,846
like a method really
with multiple arguments.

2788
01:58:08,236 --> 01:58:10,186
All right, so let's
see, initWithTitle.

2789
01:58:10,186 --> 01:58:12,786
What do I want the title
of this thing to be?

2790
01:58:13,106 --> 01:58:15,106
Let's say "Finally!"

2791
01:58:15,956 --> 01:58:18,246
What do I want the
message to be?

2792
01:58:18,366 --> 01:58:21,056
How about S 'cause I took the
time to construct S in advance.

2793
01:58:21,386 --> 01:58:22,496
Message delegate.

2794
01:58:22,496 --> 01:58:25,096
I don't want to delegate
yet so I'm actually going

2795
01:58:25,096 --> 01:58:27,706
to change this to nil for now.

2796
01:58:27,706 --> 01:58:31,306
We'll come back to that and what
it's doing, cancelButton string.

2797
01:58:31,306 --> 01:58:34,436
So cancelButton string is
essentially the dismiss button.

2798
01:58:34,596 --> 01:58:37,846
So I'm going to go ahead
and call this "Thanks!"

2799
01:58:37,906 --> 01:58:42,556
And otherButtonTitle, I'm
going to go ahead and say nil.

2800
01:58:42,556 --> 01:58:44,366
And otherButtonTitle
means if you want more

2801
01:58:44,366 --> 01:58:46,776
than just one button, this
is how you give an array

2802
01:58:46,776 --> 01:58:49,326
of buttons two and three
and four and so forth.

2803
01:58:49,916 --> 01:58:51,316
Semicolon, save.

2804
01:58:51,916 --> 01:58:54,856
OK, why is the Xcode
not happy with me now?

2805
01:58:56,016 --> 01:58:57,806
[ Inaudible Remark ]

2806
01:58:57,806 --> 01:58:58,956
I haven't used it yet.

2807
01:58:58,956 --> 01:59:02,046
I've used S now, so I've solved
one exclamation points issue.

2808
01:59:02,286 --> 01:59:03,226
But if I click on this,

2809
01:59:03,226 --> 01:59:05,776
it's going to tell me alert is
not use 'cause obviously I've

2810
01:59:05,776 --> 01:59:06,416
not used it.

2811
01:59:06,566 --> 01:59:07,356
Now this is a mess.

2812
01:59:07,356 --> 01:59:08,136
It's hard to read.

2813
01:59:08,136 --> 01:59:11,166
It turns out if you start
hitting Enter in your code,

2814
01:59:11,276 --> 01:59:14,736
what Xcode will do is
rather anally line things

2815
01:59:14,736 --> 01:59:17,526
up with the semicolons as is
the convention stylistically

2816
01:59:17,526 --> 01:59:18,346
in Objective-C.

2817
01:59:18,606 --> 01:59:20,146
So the IDE will do that for you.

2818
01:59:20,146 --> 01:59:21,976
And this is though
strange looking,

2819
01:59:21,976 --> 01:59:23,616
it's a lot more readable
than a line

2820
01:59:23,616 --> 01:59:24,656
that wraps and wraps and wraps.

2821
01:59:24,656 --> 01:59:26,166
So we'll leave it
in this fashion.

2822
01:59:26,596 --> 01:59:28,066
All right, now what
do I want to do?

2823
01:59:28,716 --> 01:59:29,886
I don't know.

2824
01:59:30,096 --> 01:59:31,536
I don't remember,
but no problem.

2825
01:59:31,536 --> 01:59:33,706
I know I have a UIAlertView
object.

2826
01:59:33,706 --> 01:59:36,516
I'm going to go ahead and click
Option, get the question mark.

2827
01:59:37,106 --> 01:59:40,016
OK, the documentation is a
little sparse here so I'm going

2828
01:59:40,016 --> 01:59:41,376
to click the class reference.

2829
01:59:41,876 --> 01:59:44,946
And now what can I do
with the UIAlertView?

2830
01:59:45,156 --> 01:59:48,066
Well, there's the method
that we described very

2831
01:59:48,066 --> 01:59:49,096
verbosely earlier.

2832
01:59:49,356 --> 01:59:51,136
It turns out there's some
properties, delegate,

2833
01:59:51,136 --> 01:59:54,976
alertViewStyle, title, message,
visible, so we've seen hints

2834
01:59:54,976 --> 01:59:56,206
of some of those existing.

2835
01:59:56,576 --> 01:59:59,496
AddButtonWithTitle,
numberOfButtons--

2836
01:59:59,496 --> 02:00:00,726
what do you like so far?

2837
02:00:01,016 --> 02:00:02,776
[ Inaudible Remark ]

2838
02:00:02,776 --> 02:00:06,406
Show. All right, so let's see
what show is defined as, show.

2839
02:00:06,796 --> 02:00:09,886
So, displays the
receiver using animation.

2840
02:00:09,886 --> 02:00:10,136
All right.

2841
02:00:10,266 --> 02:00:12,576
This is a very complex way of
saying show the alert view.

2842
02:00:12,796 --> 02:00:13,576
So let's try that.

2843
02:00:13,576 --> 02:00:16,836
This is an instance method,
returns nothing, so I just have

2844
02:00:16,836 --> 02:00:18,816
to send a super simple
message to my code.

2845
02:00:18,816 --> 02:00:21,716
So I'm going to go on my
new line here and say--

2846
02:00:21,716 --> 02:00:24,776
how do I send the show
message to this object?

2847
02:00:26,656 --> 02:00:29,256
Just alert show, all right.

2848
02:00:30,126 --> 02:00:32,076
All right, so let's
see what now happens.

2849
02:00:32,076 --> 02:00:35,276
I'm going to click
Run, Build Succeeded.

2850
02:00:35,276 --> 02:00:36,746
I'm going to go ahead and type

2851
02:00:36,746 --> 02:00:39,816
in D-A-V-I-D, and
here we go, go.

2852
02:00:40,316 --> 02:00:44,006
All right, so now with some
more respectable iOS out.

2853
02:00:44,246 --> 02:00:46,876
Finally is the title,
Hello, David is the string S

2854
02:00:46,976 --> 02:00:48,296
that I passed in as the message.

2855
02:00:48,336 --> 02:00:53,216
Thanks is the default cancel
buttons icon or text rather.

2856
02:00:53,216 --> 02:00:55,036
And if I click that and
indeed it goes away.

2857
02:00:55,276 --> 02:00:57,246
So, I can be a little
more anal here.

2858
02:00:57,426 --> 02:01:00,046
I'd like to make this a
little sexier like when I see

2859
02:01:00,046 --> 02:01:03,726
that prompt, when I click go,
why is the keyboard still there?

2860
02:01:03,966 --> 02:01:07,846
Right, like you're seeing it
through the translucent button.

2861
02:01:08,096 --> 02:01:09,076
Let's clean this up.

2862
02:01:09,296 --> 02:01:12,026
And I would only know this by
Googling around a little bit.

2863
02:01:12,056 --> 02:01:14,736
But what I'm going to do here
before we actually display

2864
02:01:14,736 --> 02:01:15,926
that alert is I'm going

2865
02:01:15,926 --> 02:01:21,396
to do self.textField
resignFirstResponder.

2866
02:01:22,046 --> 02:01:24,156
So this is instructive if only

2867
02:01:24,156 --> 02:01:26,406
because we've seen this
buzz word responder before,

2868
02:01:26,406 --> 02:01:29,176
first responder, the guy to
respond to like a key press.

2869
02:01:29,486 --> 02:01:32,016
Well, how would you say,
"I'm done responding?"

2870
02:01:32,016 --> 02:01:33,286
How do you put something away?

2871
02:01:33,596 --> 02:01:37,256
Well, if I send this message
resignFirstResponder status

2872
02:01:37,616 --> 02:01:41,136
to the text field object
to which I have a pointer

2873
02:01:41,356 --> 02:01:43,336
in that property that
I declared earlier,

2874
02:01:43,516 --> 02:01:44,936
I can tell the keyboard,
"Go away".

2875
02:01:45,496 --> 02:01:47,546
Like the user gave
focus to the text field,

2876
02:01:47,546 --> 02:01:49,626
this effectively
blurs the text field.

2877
02:01:49,626 --> 02:01:52,286
And again, it's not as
simple as focus blur,

2878
02:01:52,286 --> 02:01:53,406
I have to read the documentation

2879
02:01:53,406 --> 02:01:54,516
to know what the
message is called.

2880
02:01:54,776 --> 02:01:57,666
But if I try this now,
notice it's a little sexier.

2881
02:01:58,026 --> 02:02:00,896
D-A-V-I-D, go, and it goes away

2882
02:02:01,226 --> 02:02:03,866
because it's resigned its
first responder status.

2883
02:02:03,866 --> 02:02:06,096
If I give focus again
to the text field,

2884
02:02:06,096 --> 02:02:09,266
it comes back and
it now goes away.

2885
02:02:10,006 --> 02:02:11,776
So, there's a bug though.

2886
02:02:12,486 --> 02:02:16,826
If I go up here and I type
in PROB again and hit Enter

2887
02:02:17,206 --> 02:02:22,316
or even physically go down here,
this feels unnatural, right?

2888
02:02:22,316 --> 02:02:24,036
I shouldn't be forcing the user

2889
02:02:24,036 --> 02:02:27,566
to hit go even though they have
a return key on the keyboard.

2890
02:02:27,566 --> 02:02:29,826
So how do we go about
capturing that?

2891
02:02:30,996 --> 02:02:35,366
Well, who might know if
and when return is entered?

2892
02:02:35,556 --> 02:02:37,606
What do I want to wire
to what do you think?

2893
02:02:38,016 --> 02:02:38,136
Yeah?

2894
02:02:40,076 --> 02:02:42,066
>> The text field to go.

2895
02:02:42,326 --> 02:02:45,826
>> Yeah. So previously we wired
the go button to the go method

2896
02:02:45,826 --> 02:02:47,826
but we didn't wire the keyboard

2897
02:02:48,016 --> 02:02:51,046
or the text field rather
to that same method.

2898
02:02:51,046 --> 02:02:53,246
But the text field should
surely know when one

2899
02:02:53,246 --> 02:02:54,536
of its buttons is pressed.

2900
02:02:54,776 --> 02:02:56,786
So let's see if we
can find that.

2901
02:02:56,786 --> 02:02:58,276
Let me go into the nib file.

2902
02:02:58,736 --> 02:03:02,586
Let me go ahead and control
click on the text field and,

2903
02:03:02,586 --> 02:03:04,466
oh my goodness, so
many different things,

2904
02:03:05,036 --> 02:03:09,856
and Did End On Exit, Editing
Changed, Editing Did Begin.

2905
02:03:09,856 --> 02:03:12,356
Unfortunately, if you
just rely on the default

2906
02:03:12,356 --> 02:03:13,836
by clicking control
and dragging,

2907
02:03:14,076 --> 02:03:15,746
you don't get the right events.

2908
02:03:15,746 --> 02:03:19,176
But what I'm going to do here
is I think the one I want is Did

2909
02:03:19,176 --> 02:03:22,036
End On Exit, and again
completely unintuitive,

2910
02:03:22,036 --> 02:03:23,586
but if you read the
documentation you'd see

2911
02:03:23,586 --> 02:03:24,656
that defined.

2912
02:03:24,956 --> 02:03:28,536
Let go, select go, and let's
see if I remembered right.

2913
02:03:29,316 --> 02:03:33,916
All right, if I click run,
boot this up and I'll go ahead

2914
02:03:33,916 --> 02:03:37,656
and type in RJ this
time and enter.

2915
02:03:38,866 --> 02:03:41,656
Nice, RJ. Let's do it again.

2916
02:03:41,656 --> 02:03:46,646
RJ, no periods, click Enter,
finally, and it actually worked.

2917
02:03:46,736 --> 02:03:48,966
All right, so how
did I know this?

2918
02:03:49,066 --> 02:03:50,816
Well, honestly, it's
really by trial and error.

2919
02:03:50,816 --> 02:03:53,016
Had I done it the other way
first, let's control click,

2920
02:03:53,346 --> 02:03:54,826
let's get rid of the X there.

2921
02:03:55,016 --> 02:03:58,206
Let's just do the default and
click and drag files owner,

2922
02:03:58,206 --> 02:04:00,316
choose go, on faith, good to go.

2923
02:04:00,656 --> 02:04:01,266
Not quite.

2924
02:04:01,266 --> 02:04:02,606
'Cause if I now click here,

2925
02:04:02,816 --> 02:04:07,226
notice that it's Editing Did
End is the event that's assumed

2926
02:04:07,226 --> 02:04:10,326
to be the default by Interface
Builder, which is not correct.

2927
02:04:10,326 --> 02:04:15,606
The one I used a moment
ago was Did End On Exit.

2928
02:04:15,606 --> 02:04:17,106
Again, completely unintuitive,

2929
02:04:17,326 --> 02:04:18,686
and there's a slight
distinction.

2930
02:04:18,686 --> 02:04:22,706
Editing Did End is what gets
fired when you take focus away

2931
02:04:22,706 --> 02:04:24,506
from the text field
but the catch is

2932
02:04:24,506 --> 02:04:25,916
that hitting Enter
doesn't take--

2933
02:04:25,916 --> 02:04:27,496
technically take focus away.

2934
02:04:27,706 --> 02:04:30,666
What happens when you hit Enter
is an event called Did End

2935
02:04:30,736 --> 02:04:31,216
On Exit.

2936
02:04:31,216 --> 02:04:32,856
And I mention this
'cause honestly,

2937
02:04:32,856 --> 02:04:35,126
the first experience
you'll have hands on in lab

2938
02:04:35,126 --> 02:04:36,116
and with the first project,

2939
02:04:36,316 --> 02:04:38,896
there's all these stupid little
things that are very easy

2940
02:04:38,896 --> 02:04:41,636
to trip over even when you
have a super simple application

2941
02:04:41,816 --> 02:04:44,476
and I can't emphasize enough
when first acclimating to this

2942
02:04:44,606 --> 02:04:46,976
to really just kind
of retrace your steps,

2943
02:04:47,006 --> 02:04:48,546
go back to the sample
code from lecture,

2944
02:04:48,546 --> 02:04:50,486
figure out what's
different between your code

2945
02:04:50,486 --> 02:04:52,726
and say the lecture
code, and you'll find

2946
02:04:52,726 --> 02:04:54,426
that it's just these
little tips and tricks

2947
02:04:54,426 --> 02:04:57,046
that you'll stumble upon so that
you don't make those mistakes

2948
02:04:57,256 --> 02:04:58,686
again the second time around.

2949
02:04:59,176 --> 02:04:59,456
Yes?

2950
02:05:00,516 --> 02:05:02,576
[ Inaudible Remark ]

2951
02:05:03,076 --> 02:05:04,066
OK.

2952
02:05:05,241 --> 02:05:07,241
[ Inaudible Remark ]

2953
02:05:07,466 --> 02:05:09,736
Very good question.

2954
02:05:10,026 --> 02:05:12,046
So let's go back to
my ViewController.m.

2955
02:05:12,526 --> 02:05:15,076
If I had code executed
right here,

2956
02:05:16,146 --> 02:05:17,866
would it get executed
immediately?

2957
02:05:17,966 --> 02:05:19,196
Short answer, yes.

2958
02:05:19,326 --> 02:05:22,466
So, we can actually think back
to our discussions of Ajax.

2959
02:05:22,766 --> 02:05:24,156
This is an asynchronous call

2960
02:05:24,606 --> 02:05:27,376
which means the message
show is sent immediately

2961
02:05:27,376 --> 02:05:29,876
to the alert object,
but the next line

2962
02:05:29,876 --> 02:05:31,576
of code will immediately
execute.

2963
02:05:31,736 --> 02:05:32,716
And it turns out that

2964
02:05:32,716 --> 02:05:35,666
that message box may
actually take a split second

2965
02:05:35,666 --> 02:05:37,176
to actually appear.

2966
02:05:37,536 --> 02:05:42,376
So if you want to actually
respond to the dismissing

2967
02:05:42,566 --> 02:05:45,076
of that text, of
that alert view,

2968
02:05:45,496 --> 02:05:48,296
you have to adopt
something known as a delegate

2969
02:05:48,836 --> 02:05:50,876
and use a protocol of sorts.

2970
02:05:51,236 --> 02:05:52,856
We'll come back to
that in just a moment

2971
02:05:52,856 --> 02:05:55,186
and that's again much like Ajax.

2972
02:05:55,186 --> 02:05:57,466
These are asynchronous
operations, and we'll have

2973
02:05:57,466 --> 02:05:59,236
to inform the class
if we want it

2974
02:05:59,236 --> 02:06:01,086
to tell us something in return.

2975
02:06:01,586 --> 02:06:04,006
Other questions?

2976
02:06:05,456 --> 02:06:07,736
So let's propose a
cliffhanger here?

2977
02:06:07,916 --> 02:06:12,716
Suppose that the functionality
I want to implement next is

2978
02:06:13,156 --> 02:06:16,496
when I type in something
like D-A-V-I-D and click go,

2979
02:06:16,766 --> 02:06:18,226
I like the keyboard going away,

2980
02:06:18,456 --> 02:06:20,076
I like the message
then coming up.

2981
02:06:20,356 --> 02:06:24,056
What I don't like now,
anal as I'm feeling here,

2982
02:06:24,056 --> 02:06:27,246
is that when I click thanks,
I don't get the keyboard back.

2983
02:06:27,436 --> 02:06:28,566
It still says David.

2984
02:06:28,566 --> 02:06:30,336
This is really kind of
a sloppy application.

2985
02:06:30,336 --> 02:06:33,246
I'd really like focus to be
given back to the text field.

2986
02:06:33,246 --> 02:06:34,706
I want to see the
placeholder text again

2987
02:06:34,706 --> 02:06:36,856
and I want my cursor
flashing there

2988
02:06:36,856 --> 02:06:38,396
for the next round of this game.

2989
02:06:38,686 --> 02:06:41,886
So in other words, we need now
per your comment a moment ago,

2990
02:06:42,056 --> 02:06:45,036
we need some way of
having the dismissal

2991
02:06:45,036 --> 02:06:48,906
of that alert view
window signal to my code

2992
02:06:49,296 --> 02:06:50,716
to do those cleanup steps.

2993
02:06:50,806 --> 02:06:52,386
Give me the keyboard
back, give me focus back,

2994
02:06:52,386 --> 02:06:53,936
delete David and start anew.

2995
02:06:54,296 --> 02:06:56,236
Let's go ahead and take our
second five-minute break here

2996
02:06:56,236 --> 02:06:57,726
and we'll come back
and solve that problem.

2997
02:06:58,136 --> 02:07:02,166
All right, I know it's
late but we're almost there

2998
02:07:02,516 --> 02:07:04,396
and this next stuff
will be amazing.

2999
02:07:04,906 --> 02:07:09,946
But it will make
you better at this.

3000
02:07:09,946 --> 02:07:11,386
All right, so we are back.

3001
02:07:11,546 --> 02:07:12,786
So where did we just leave off?

3002
02:07:12,786 --> 02:07:15,456
So we have an implementation
of view controller now

3003
02:07:15,666 --> 02:07:17,456
in both our H file
and our M file

3004
02:07:17,456 --> 02:07:18,696
that does relatively little.

3005
02:07:18,696 --> 02:07:21,506
In the H file we have a
property to that text field.

3006
02:07:21,976 --> 02:07:24,966
Notice we don't have a
property to the go button.

3007
02:07:25,176 --> 02:07:27,496
Why don't we need a
property to the go button?

3008
02:07:28,516 --> 02:07:31,876
[ Inaudible Remark ]

3009
02:07:32,376 --> 02:07:35,376
It's not an IBOutlet and we
don't really need anything--

3010
02:07:35,696 --> 02:07:37,726
we don't need to pull
anything from it.

3011
02:07:37,726 --> 02:07:40,066
It in a sense is going
to push information

3012
02:07:40,066 --> 02:07:41,506
to us when it's clicked.

3013
02:07:41,576 --> 02:07:43,136
So we don't need a
property for that.

3014
02:07:43,136 --> 02:07:44,986
And if I look now at my M file,

3015
02:07:45,306 --> 02:07:48,116
notice that all I've
implemented is that IBAction

3016
02:07:48,376 --> 02:07:51,206
and that IBAction is called
go and it just does all

3017
02:07:51,206 --> 02:07:52,656
that UIAlertView stuff.

3018
02:07:52,656 --> 02:07:54,936
But what we don't yet have
is the ability to detect

3019
02:07:55,226 --> 02:07:56,506
when the user dismisses

3020
02:07:56,906 --> 02:07:59,736
that UIAlertView
button labeled thanks.

3021
02:07:59,866 --> 02:08:04,526
As an aside, IBAction and
IBOutlet, what are these things?

3022
02:08:04,526 --> 02:08:07,326
It turns out it's just
a very clever typedef.

3023
02:08:07,626 --> 02:08:11,016
Typedef is a way of declaring
a synonym in C, in Objective-C,

3024
02:08:11,166 --> 02:08:13,776
whereby you can give a
new name to a data type.

3025
02:08:14,086 --> 02:08:17,686
And so IBAction is actually
literally a synonym for void,

3026
02:08:17,926 --> 02:08:20,516
and void means a
nonexistent return type.

3027
02:08:20,516 --> 02:08:22,166
It doesn't return
an actual value.

3028
02:08:22,426 --> 02:08:26,196
So all Apple has done is they
have declared that IBoutlet

3029
02:08:26,196 --> 02:08:29,656
and IBAction are
synonyms for void.

3030
02:08:29,976 --> 02:08:32,926
So the effect of this is to
have no functional impact

3031
02:08:32,926 --> 02:08:33,566
on your code.

3032
02:08:33,566 --> 02:08:34,686
The compiler doesn't care.

3033
02:08:34,816 --> 02:08:38,046
But what Xcode does is it uses
regular expressions essentially,

3034
02:08:38,226 --> 02:08:41,026
skims your code and
anywhere it sees an IBAction,

3035
02:08:41,226 --> 02:08:43,756
it realizes that it can put
this cute little circle here

3036
02:08:43,756 --> 02:08:46,846
and it can put that name go
into the little dropdown menu.

3037
02:08:47,196 --> 02:08:51,356
Same thing in the H file for
the IBOutlet, this too is just--

3038
02:08:51,356 --> 02:08:54,546
this actually isn't void,
this is quote unquote.

3039
02:08:55,006 --> 02:08:57,276
So literally it's a
synonym for nothing.

3040
02:08:57,276 --> 02:08:59,006
So, if the compiler
gets rid of that,

3041
02:08:59,216 --> 02:09:01,156
it has no functional
effect but it does know

3042
02:09:01,156 --> 02:09:03,286
to put the little cute circle
and to put the text field

3043
02:09:03,286 --> 02:09:05,126
in the dropdown that we
saw when I was dragging

3044
02:09:05,126 --> 02:09:06,586
and dropping the blue line.

3045
02:09:06,586 --> 02:09:08,896
So it's kind of a
clever hack, if you will,

3046
02:09:09,076 --> 02:09:11,166
to make the Interface
Builder work without having

3047
02:09:11,166 --> 02:09:13,206
to fundamentally
change the language.

3048
02:09:13,436 --> 02:09:16,996
All right, so let's now wire
together the thanks button

3049
02:09:16,996 --> 02:09:23,446
with our code in a way that
allows us to round off the last

3050
02:09:23,666 --> 02:09:26,606
of the technical topics from
the very start of lecture.

3051
02:09:26,606 --> 02:09:28,296
So we looked at categories
briefly,

3052
02:09:28,516 --> 02:09:30,306
let's lastly look at protocol.

3053
02:09:30,306 --> 02:09:32,756
I'm going to go into some of
the prefabbed code from tonight

3054
02:09:33,066 --> 02:09:35,596
into a file called Nib2
so that we don't have

3055
02:09:35,596 --> 02:09:37,576
to rewrite all of
that code again.

3056
02:09:37,796 --> 02:09:40,716
Nib2 I'll tell you
upfront is almost identical

3057
02:09:40,716 --> 02:09:42,516
to the previous example except

3058
02:09:42,936 --> 02:09:44,976
that in advance I have
solved this problem

3059
02:09:44,976 --> 02:09:48,376
of wiring the thanks the
button up with my actual code.

3060
02:09:48,626 --> 02:09:49,086
So let's look

3061
02:09:49,086 --> 02:09:53,496
at ViewController.h. Do you see
anything different in this file?

3062
02:09:54,516 --> 02:09:59,116
[ Pause ]

3063
02:09:59,616 --> 02:10:00,446
What's different?

3064
02:10:00,836 --> 02:10:00,916
Yeah.

3065
02:10:01,516 --> 02:10:03,696
[ Inaudible Remark ]

3066
02:10:04,196 --> 02:10:07,166
Exactly. So we have
UIAlertViewDelegate

3067
02:10:07,166 --> 02:10:10,266
which wasn't there before in
the previous example, Nib1.

3068
02:10:10,616 --> 02:10:14,066
This file was almost the
same but that was absent.

3069
02:10:14,516 --> 02:10:16,656
So the fact that I have
UIAlertViewDelegate

3070
02:10:16,656 --> 02:10:20,516
in angle brackets just means
that my class, view controller

3071
02:10:20,816 --> 02:10:23,906
that descends from
UIViewController is also going

3072
02:10:23,906 --> 02:10:27,386
to implement some methods
declared in that protocol.

3073
02:10:27,386 --> 02:10:28,666
Now, I forget what
they're called

3074
02:10:28,666 --> 02:10:29,936
but this is easily solved.

3075
02:10:30,076 --> 02:10:33,716
Let me hold down option,
click on UIAlertViewDelegate,

3076
02:10:33,716 --> 02:10:36,436
click on the reference,
bring out the documentation.

3077
02:10:36,566 --> 02:10:39,956
And if I read this
closely, you'll notice that,

3078
02:10:40,776 --> 02:10:45,286
you'll notice that if
you add your own button--

3079
02:10:45,286 --> 02:10:46,226
no, not that one.

3080
02:10:46,676 --> 02:10:47,846
What do I want here?

3081
02:10:47,946 --> 02:10:48,866
Scroll down.

3082
02:10:49,186 --> 02:10:54,706
This one, alertView
didDismissWithButtonIndex.

3083
02:10:54,906 --> 02:10:57,596
That is apparently, if I read
the documentation closely,

3084
02:10:57,916 --> 02:11:01,576
that is the method that will be
called when the user did dismiss

3085
02:11:01,576 --> 02:11:03,446
with a button index of zero,

3086
02:11:03,446 --> 02:11:05,936
in the default case,
that thanks button.

3087
02:11:06,266 --> 02:11:09,126
So I just have to implement
that now to be able to detect

3088
02:11:09,356 --> 02:11:11,856
with an event listener that
the button has been pressed.

3089
02:11:12,006 --> 02:11:14,526
So let me go into my M file
and see how I've done this.

3090
02:11:15,336 --> 02:11:16,686
So what did I do here?

3091
02:11:16,686 --> 02:11:17,396
Interesting.

3092
02:11:17,596 --> 02:11:21,856
So, alertView
didDismissWithButtonIndex.

3093
02:11:21,976 --> 02:11:23,346
What did I decide to do?

3094
02:11:23,706 --> 02:11:26,256
So my comment is kind of
spoiling the fun here.

3095
02:11:26,606 --> 02:11:30,166
But when that UIAlertView
sends this message

3096
02:11:30,166 --> 02:11:33,646
to my view controller object,
I'm executing one line of code

3097
02:11:33,646 --> 02:11:36,046
which apparently does what?

3098
02:11:36,246 --> 02:11:37,356
Clears the text field.

3099
02:11:37,356 --> 02:11:38,096
How do I do that?

3100
02:11:38,096 --> 02:11:41,546
I can simply set the property
called text that's associated

3101
02:11:41,546 --> 02:11:44,786
with my text field to nil
quote unquote effectively.

3102
02:11:44,786 --> 02:11:47,266
Or setting it to nil is that--
because of the setter it knows

3103
02:11:47,266 --> 02:11:49,106
to set it effectively
to quote unquote.

3104
02:11:49,336 --> 02:11:51,786
So that's how I clear it so
that I see the placeholder text

3105
02:11:51,786 --> 02:11:54,136
again, not D-A-V-I-D.

3106
02:11:54,376 --> 02:11:58,796
And then lastly-- this actually
I did not mean to leave this

3107
02:11:58,796 --> 02:11:59,766
in the distribution code.

3108
02:11:59,766 --> 02:12:01,156
This was me tinkering
around before.

3109
02:12:01,416 --> 02:12:02,506
I will remove this.

3110
02:12:02,506 --> 02:12:04,966
If I leave that in there, as
soon as the alert view comes up,

3111
02:12:04,966 --> 02:12:06,636
it goes away 'cause
I was experimenting

3112
02:12:06,636 --> 02:12:07,996
with a different example.

3113
02:12:08,416 --> 02:12:11,046
All right, I'll fix that on the
online source code eventually.

3114
02:12:12,116 --> 02:12:15,396
So, there's one other
difference between this file

3115
02:12:15,396 --> 02:12:17,206
and the last one but
it's a little subtle.

3116
02:12:17,656 --> 02:12:22,796
What else have I done
that's different?

3117
02:12:23,226 --> 02:12:24,256
Yeah, excellent.

3118
02:12:24,256 --> 02:12:26,016
So, whereas last time the value

3119
02:12:26,016 --> 02:12:30,296
of delegate was nil,
this time it's self.

3120
02:12:30,646 --> 02:12:33,676
So this is a very common
paradigm in iOS whereby

3121
02:12:33,676 --> 02:12:36,316
when you call a method
on some other object,

3122
02:12:36,466 --> 02:12:37,656
in this case the object

3123
02:12:37,656 --> 02:12:41,536
in question is this UIAlertView
object, and you want to inform

3124
02:12:41,536 --> 02:12:44,646
that guy to whom he
should send the message

3125
02:12:44,646 --> 02:12:47,606
when he's done doing
something, you can pass

3126
02:12:47,606 --> 02:12:50,826
in a delegate reference, a
pointer to some other object.

3127
02:12:50,956 --> 02:12:53,506
In this case, who do
I want to be informed

3128
02:12:53,506 --> 02:12:56,496
when that button is clicked?

3129
02:12:56,656 --> 02:13:00,636
My self. And literally,
I pass in therefore self.

3130
02:13:00,936 --> 02:13:06,036
Now this works because the
UIAlertView object knows

3131
02:13:06,346 --> 02:13:10,626
to send the message called
alertView didDismissWithIndex

3132
02:13:10,856 --> 02:13:13,326
to its delegate.

3133
02:13:14,096 --> 02:13:16,416
If it's nil, no big deal,
it's going to send the message

3134
02:13:16,416 --> 02:13:19,136
into a black hole so no
one hears that message.

3135
02:13:19,326 --> 02:13:21,936
But if I'm telling it
that I am your delegate,

3136
02:13:21,936 --> 02:13:24,846
let me know when you're
done being clicked on,

3137
02:13:25,106 --> 02:13:27,706
send me a message, I just
have to pass in self.

3138
02:13:27,746 --> 02:13:32,476
And because I have implemented
this method now, I can hear it.

3139
02:13:32,986 --> 02:13:35,256
If I get rid of that, the
message might still be sent

3140
02:13:35,256 --> 02:13:38,806
to me but it's just going to
go into a black hole as well.

3141
02:13:39,046 --> 02:13:40,506
But here I've intercepted
it much

3142
02:13:40,506 --> 02:13:42,876
like registering an event
listener with JavaScript

3143
02:13:42,876 --> 02:13:46,086
or jQuery in the
most recent project.

3144
02:13:46,286 --> 02:13:46,426
Yeah.

3145
02:13:47,516 --> 02:13:50,736
[ Inaudible Remark ]

3146
02:13:51,236 --> 02:13:55,846
Who's holding a pointer
to the alert when the--

3147
02:13:56,106 --> 02:14:00,186
so we have in this line here,

3148
02:14:00,586 --> 02:14:02,696
so this is where automatic
reference counting comes

3149
02:14:02,696 --> 02:14:03,146
into play.

3150
02:14:03,586 --> 02:14:08,276
So, we have here an allocation
of the UIAlertView object.

3151
02:14:08,276 --> 02:14:09,436
We have a pointer here.

3152
02:14:09,706 --> 02:14:11,316
We're then sending the message.

3153
02:14:11,636 --> 02:14:15,456
Essentially what is happening
here is the operating system is

3154
02:14:15,456 --> 02:14:20,426
going to keep track of who still
has a pointer to that object.

3155
02:14:20,786 --> 02:14:24,516
And because this is the only guy
in existence that has a pointer

3156
02:14:24,516 --> 02:14:26,086
to that object, effectively
when it goes

3157
02:14:26,086 --> 02:14:27,526
out of scope it will
be reclaimed

3158
02:14:27,526 --> 02:14:28,876
when it's no longer in reuse.

3159
02:14:30,026 --> 02:14:32,666
And let me wave my hand at
the memory management details

3160
02:14:32,666 --> 02:14:33,946
for now 'cause we'll
come back to this

3161
02:14:34,156 --> 02:14:36,886
in particular next week
especially the notion of weak

3162
02:14:37,006 --> 02:14:39,286
and strong as it might
relate to something like this

3163
02:14:39,286 --> 02:14:40,286
where you're passing a pointer

3164
02:14:40,556 --> 02:14:42,626
from yourself to
some other object.

3165
02:14:42,986 --> 02:14:44,316
But this is some of the magic

3166
02:14:44,316 --> 02:14:45,616
that now happens
underneath the hood

3167
02:14:45,726 --> 02:14:47,776
because of that feature ARC.

3168
02:14:48,436 --> 02:14:51,006
Previously we would have had to
free it eventually ourselves.

3169
02:14:51,386 --> 02:14:54,276
Technically there's an
autorelease pool that does this

3170
02:14:54,276 --> 02:14:57,366
for us and automatically
frees the memory eventually.

3171
02:14:58,386 --> 02:15:02,156
All right, so let's take
this up one more notch.

3172
02:15:02,516 --> 02:15:04,896
I'm going to kind of whiz
through this code just

3173
02:15:04,896 --> 02:15:09,016
so that we've-- you don't feel
that you're entirely dependent

3174
02:15:09,946 --> 02:15:11,826
on this Interface Builder.

3175
02:15:11,926 --> 02:15:15,636
Let me open up NonIB.Xcode
project.

3176
02:15:15,636 --> 02:15:18,686
And in this example, if I
expand the files over here,

3177
02:15:18,686 --> 02:15:22,576
notice that we again have main.m
and let me emphasize here,

3178
02:15:22,576 --> 02:15:24,256
when in doubt when
reading someone's code

3179
02:15:24,256 --> 02:15:25,996
for the first time,
certainly lecture examples

3180
02:15:25,996 --> 02:15:28,936
and even Apple's or others,
then it gets helpful to start

3181
02:15:28,936 --> 02:15:31,206
with main.m just to
reassure yourself, OK,

3182
02:15:31,206 --> 02:15:32,226
familiar, nothing there.

3183
02:15:32,446 --> 02:15:34,046
Let me now follow
the breadcrumbs

3184
02:15:34,046 --> 02:15:35,166
and go to AppDelegate.

3185
02:15:35,166 --> 02:15:38,346
OK, now I'm going to
AppDelegate.h. Nothing new here,

3186
02:15:38,346 --> 02:15:40,756
this looks like the
example from awhile ago,

3187
02:15:40,756 --> 02:15:43,166
ViewController pointer
and a window pointer.

3188
02:15:43,506 --> 02:15:47,416
AppDelegate.m, no, nothing
interesting in here.

3189
02:15:47,416 --> 02:15:49,526
It looks like the brains
of the operation are indeed

3190
02:15:49,526 --> 02:15:50,326
in the ViewController.

3191
02:15:50,326 --> 02:15:53,466
Let's look at the dot H.
OK, little interesting here,

3192
02:15:53,466 --> 02:15:56,396
alertView
didDismissWithButtonIndex go.

3193
02:15:56,576 --> 02:15:57,956
OK, that's actually
the same as before.

3194
02:15:57,956 --> 02:15:59,956
Notice I'm being explicit
as someone asked earlier

3195
02:15:59,956 --> 02:16:01,706
with read/write even
though that's the default,

3196
02:16:01,806 --> 02:16:02,806
I could say read-only.

3197
02:16:03,586 --> 02:16:05,886
ViewController.m. So
here's the difference,

3198
02:16:05,926 --> 02:16:09,216
and you can immediately see
why I used Interface Builder

3199
02:16:09,246 --> 02:16:09,766
at first.

3200
02:16:10,006 --> 02:16:12,326
This is the code with which
I've implemented things

3201
02:16:12,456 --> 02:16:13,186
without a nib.

3202
02:16:13,456 --> 02:16:18,096
Notice missing at top left
is ViewController.nib,

3203
02:16:18,206 --> 02:16:20,466
so I'm doing it sort
of the real way

3204
02:16:20,686 --> 02:16:25,476
like without using a little
drag and drop WYSIWYG tool.

3205
02:16:25,756 --> 02:16:28,046
So, the price you pay is
that it's a pain in the ass

3206
02:16:28,046 --> 02:16:30,176
and so here is the number of
lines of code I've written.

3207
02:16:30,286 --> 02:16:30,796
And this is a bit

3208
02:16:30,796 --> 02:16:32,616
of an exaggeration
'cause I could have relied

3209
02:16:32,616 --> 02:16:35,046
on some defaults for some of the
properties you're about to see.

3210
02:16:35,246 --> 02:16:38,906
But I included them just to make
explicit what the code looks

3211
02:16:38,906 --> 02:16:40,686
like for a lot of
those Photoshop

3212
02:16:40,686 --> 02:16:43,756
like attribute settings that
I was so pleasantly clicking

3213
02:16:43,756 --> 02:16:44,916
and dragging on earlier.

3214
02:16:45,276 --> 02:16:48,416
So, loadView, if we read the
documentation is a method

3215
02:16:48,736 --> 02:16:50,816
that comes with the
UIViewController class

3216
02:16:51,396 --> 02:16:52,316
from which I descend.

3217
02:16:52,886 --> 02:16:56,056
And he load-- is responsible
for loading the graphics

3218
02:16:56,056 --> 02:16:58,666
on to the screen if you're
not using a nib file.

3219
02:16:58,956 --> 02:17:01,836
So because we have no nib, I
have to do everything manually.

3220
02:17:01,836 --> 02:17:02,886
But it's pretty readable,

3221
02:17:02,886 --> 02:17:05,276
and I've certainly
commented it succinctly

3222
02:17:05,276 --> 02:17:06,476
but hopefully clearly.

3223
02:17:06,696 --> 02:17:07,476
Create view.

3224
02:17:07,596 --> 02:17:09,396
How do you create the
view, how do you create

3225
02:17:09,396 --> 02:17:11,206
that default rectangle on top

3226
02:17:11,206 --> 02:17:13,326
of which we start putting
all of the other objects?

3227
02:17:13,706 --> 02:17:14,936
I do self.view.

3228
02:17:15,106 --> 02:17:17,396
I allocate a UIView
and I initialize it

3229
02:17:17,396 --> 02:17:19,046
with the frame much
like we did before.

3230
02:17:19,266 --> 02:17:21,316
Slightly different
application frame

3231
02:17:21,616 --> 02:17:24,366
but it's essentially
a boilerplate code

3232
02:17:24,366 --> 02:17:26,576
from documentation that
I learned from initially.

3233
02:17:26,576 --> 02:17:28,266
And that just gives
me that rectangle.

3234
02:17:28,656 --> 02:17:31,056
Self.view.backgroundColor
gets white color.

3235
02:17:31,056 --> 02:17:32,406
That's how I make
the background white.

3236
02:17:32,406 --> 02:17:33,866
So we've seen something
like that before.

3237
02:17:34,086 --> 02:17:35,376
Now, it gets a little annoying.

3238
02:17:35,376 --> 02:17:37,696
I have to use the
Core Graphics Library.

3239
02:17:38,036 --> 02:17:41,146
Recall that under frameworks,
we had CoreGraphics.framework.

3240
02:17:41,356 --> 02:17:43,026
Well, it turns out all
these little buttons,

3241
02:17:43,026 --> 02:17:45,366
even though they're kind
of ovular little curvy,

3242
02:17:45,446 --> 02:17:47,086
I mean they're two-dimensional
graphics.

3243
02:17:47,086 --> 02:17:50,326
And they come-- there's from
lines of code like this.

3244
02:17:50,856 --> 02:17:55,266
I create a CGRect frame
by calling CGRectMake.

3245
02:17:55,396 --> 02:17:57,936
So the Core Graphics Library
turns out as all in C.

3246
02:17:57,936 --> 02:17:59,366
So now we're using C code.

3247
02:17:59,366 --> 02:18:02,456
This is not object oriented
code per se though it's kind

3248
02:18:02,456 --> 02:18:04,396
of commingled as you
can see in line two here

3249
02:18:04,396 --> 02:18:05,886
with UITextField alloc.

3250
02:18:06,256 --> 02:18:08,266
But let me wave my hand
at some of these details

3251
02:18:08,596 --> 02:18:09,716
but to emphasize this.

3252
02:18:10,786 --> 02:18:12,576
I looked at Xcode.

3253
02:18:13,136 --> 02:18:15,816
I looked at Interface Builder
from the previous example

3254
02:18:16,106 --> 02:18:19,046
and just determine by looking
at the little GUI settings

3255
02:18:19,306 --> 02:18:21,986
that that text field
was 20 pixels down,

3256
02:18:22,346 --> 02:18:24,706
20 pixels over, hence
the 20, 20.

3257
02:18:25,036 --> 02:18:28,076
I also looked at the settings
and it said that the size

3258
02:18:28,076 --> 02:18:30,096
that I created by dragging
and dropping the left

3259
02:18:30,096 --> 02:18:32,336
and right happened to
end up being 280 pixels.

3260
02:18:32,376 --> 02:18:35,826
So I copied that and it happened
to be 31 pixels high by default.

3261
02:18:35,976 --> 02:18:37,486
So I really just manually

3262
02:18:37,486 --> 02:18:39,426
and arbitrarily came
up with those values.

3263
02:18:39,796 --> 02:18:42,696
Now, I'm initializing the
text field with the frame.

3264
02:18:42,696 --> 02:18:46,836
So I have to somehow tell the
UITextField where it belongs

3265
02:18:46,956 --> 02:18:49,716
in the canvas that I'm
painting with my lines of code.

3266
02:18:50,096 --> 02:18:52,746
The rest of these lines
here, autocapitalizationType,

3267
02:18:52,746 --> 02:18:56,036
autocorrectionType, these are
just essentially properties

3268
02:18:56,036 --> 02:18:59,856
that I'm setting to constants,
to turn on or off capitalization

3269
02:18:59,856 --> 02:19:02,386
and other UI features like that,
things that I would have gotten

3270
02:19:02,386 --> 02:19:04,016
by checking boxes earlier.

3271
02:19:04,396 --> 02:19:07,406
And then placeholder,
notice I get "Name".

3272
02:19:07,686 --> 02:19:12,066
And here now, here is where
I implement those blue lines.

3273
02:19:12,686 --> 02:19:14,136
It's a little cryptic
and it's OK

3274
02:19:14,136 --> 02:19:15,676
if you're not wholly
comfortable with this just

3275
02:19:15,676 --> 02:19:17,666
yet 'cause using Interface
Builder is totally fine

3276
02:19:17,666 --> 02:19:20,406
and frankly I think it's the
better way to learn initially.

3277
02:19:21,926 --> 02:19:27,236
Self.textField addTarget
self action selector

3278
02:19:27,236 --> 02:19:28,856
go forControlEvents.

3279
02:19:29,146 --> 02:19:30,126
OK, this is a mouthful.

3280
02:19:30,126 --> 02:19:31,256
And this is not uncommon.

3281
02:19:31,256 --> 02:19:34,566
In Apple, their constants
are very, very, very long

3282
02:19:34,566 --> 02:19:35,866
and their name is
based effectively

3283
02:19:35,866 --> 02:19:37,366
by having common prefixes.

3284
02:19:37,836 --> 02:19:39,046
So what's going on here?

3285
02:19:39,456 --> 02:19:42,636
I am sending a message
to the text field to add

3286
02:19:42,636 --> 02:19:47,226
to it a pointer, a reference to
myself specifically telling it

3287
02:19:47,226 --> 02:19:50,166
to call the go method,
otherwise known as the selector,

3288
02:19:50,896 --> 02:19:52,936
whenever what event happens?

3289
02:19:54,676 --> 02:19:58,056
There is that Did
End On Exit events.

3290
02:19:58,056 --> 02:20:00,416
Recall that Interface
Builder kind of dumbed it down

3291
02:20:00,416 --> 02:20:03,086
and simplifies it, made it look
grammatically nice with spaces

3292
02:20:03,086 --> 02:20:07,436
and whatnot, but the constant in
code that represents the Did End

3293
02:20:07,436 --> 02:20:08,976
On Exit event is
expressed this way.

3294
02:20:09,146 --> 02:20:11,206
And it's just a number
underneath the hood

3295
02:20:11,246 --> 02:20:12,916
and it's a constant
that I would only know

3296
02:20:12,916 --> 02:20:14,166
by reading the documentation.

3297
02:20:14,466 --> 02:20:18,356
So, these lines of code here
that wrapped under two lines,

3298
02:20:18,676 --> 02:20:21,386
this is the equivalent of
the blue line that I drew

3299
02:20:21,876 --> 02:20:25,306
from my text field
to my file's owner.

3300
02:20:26,386 --> 02:20:27,046
All right.

3301
02:20:27,316 --> 02:20:28,786
Add text field to view.

3302
02:20:28,916 --> 02:20:30,656
So, this line of code,

3303
02:20:31,236 --> 02:20:33,656
addSubview is taking
the text field,

3304
02:20:33,996 --> 02:20:36,816
taking that default view
that's white on the bottom

3305
02:20:37,016 --> 02:20:38,576
and putting one on
top of the other.

3306
02:20:38,686 --> 02:20:40,076
That's what adding subview does.

3307
02:20:40,076 --> 02:20:42,466
It just adds an object on top
like you're plopping things

3308
02:20:42,466 --> 02:20:43,686
down on a transparency.

3309
02:20:44,066 --> 02:20:46,326
And now, this is actually
very similar to before.

3310
02:20:46,326 --> 02:20:48,976
The button I create by
creating another rectangle,

3311
02:20:48,976 --> 02:20:51,226
these numbers I figured out
just by looking at what the size

3312
02:20:51,226 --> 02:20:53,146
of my go button was in
the previous example,

3313
02:20:53,276 --> 02:20:54,856
could have come up
with any other numbers.

3314
02:20:55,356 --> 02:20:57,986
Notice that I instantiate
a UIButton

3315
02:20:58,276 --> 02:21:02,386
of type UIButtonTypeRoundedRect,
that's the rounded rectangle

3316
02:21:02,386 --> 02:21:04,356
that I so easily dragged
and dropped earlier.

3317
02:21:04,746 --> 02:21:07,936
I set what's called the buttons
frame, so what's the rectangle

3318
02:21:07,936 --> 02:21:09,706
in which this button
is going to live.

3319
02:21:09,966 --> 02:21:14,546
I then set the title here for
UI state-- UIControlStateNormal.

3320
02:21:14,546 --> 02:21:15,686
So buttons have different
states.

3321
02:21:15,686 --> 02:21:17,796
If you touch and hold,
becomes blue usually,

3322
02:21:17,796 --> 02:21:18,696
that's a different state.

3323
02:21:18,936 --> 02:21:21,406
Just the normal state is
going to be that constant.

3324
02:21:21,796 --> 02:21:24,466
And then here is how I
drew the other blue line.

3325
02:21:24,846 --> 02:21:26,716
When I dragged from
file's owner--

3326
02:21:27,026 --> 02:21:30,366
sorry, when I dragged from
the button to file's owner,

3327
02:21:30,886 --> 02:21:33,556
this is how I implemented
that other blue line.

3328
02:21:33,556 --> 02:21:35,996
As for the rest of
the code, thankfully,

3329
02:21:36,346 --> 02:21:37,766
this is the same as before.

3330
02:21:38,036 --> 02:21:39,236
This is the same as before.

3331
02:21:39,236 --> 02:21:40,906
Everything else is the same.

3332
02:21:41,176 --> 02:21:44,056
Now, there is one new
feature here, these pragmas,

3333
02:21:44,436 --> 02:21:45,926
these preprocessor directives.

3334
02:21:46,296 --> 02:21:48,246
These are just kind of a
fluffy feature, kind of nice.

3335
02:21:48,666 --> 02:21:51,306
I haven't used this before but
notice there's a little set

3336
02:21:51,306 --> 02:21:53,936
of breadcrumbs up here
that tell me where I am.

3337
02:21:54,286 --> 02:21:57,176
If I click on, let's see,
the rightmost one here,

3338
02:21:57,456 --> 02:21:59,606
notice that it's kind
of organized, actions,

3339
02:21:59,856 --> 02:22:02,526
UIAlertViewDelegate with
the bold facing and whatnot.

3340
02:22:02,926 --> 02:22:03,996
That's coming from this.

3341
02:22:04,746 --> 02:22:06,136
That's coming from this.

3342
02:22:06,136 --> 02:22:07,886
It is purely aesthetic
just to kind

3343
02:22:07,886 --> 02:22:10,816
of keep your methods organized
if you care to do that.

3344
02:22:11,286 --> 02:22:13,766
It has no functional
effect on your code.

3345
02:22:15,866 --> 02:22:16,626
Questions?

3346
02:22:16,756 --> 02:22:21,796
So in the upcoming project,
draft a few spec is online.

3347
02:22:22,136 --> 02:22:23,926
You'll be implementing
a program,

3348
02:22:23,926 --> 02:22:25,666
recall, named Evil Hangman.

3349
02:22:26,136 --> 02:22:29,296
Evil Hangman is much like
the normal hangman except

3350
02:22:29,296 --> 02:22:31,426
that it's evil in the
sense that it cheats.

3351
02:22:31,656 --> 02:22:33,866
So whereas in normal
hangman, you might choose

3352
02:22:33,866 --> 02:22:36,516
like a six-letter word and
give blank spaces for each

3353
02:22:36,516 --> 02:22:40,346
of those letters and the human
has to guess E or A or C or D

3354
02:22:40,346 --> 02:22:43,486
or whatever letters and you
gradually reveal those letters

3355
02:22:43,486 --> 02:22:45,606
as the user guesses your
word piece by piece.

3356
02:22:45,916 --> 02:22:48,686
Evil Hangman is a little
different in that, recall,

3357
02:22:48,836 --> 02:22:52,736
if I guess E, even if
the computer was thinking

3358
02:22:52,776 --> 02:22:56,096
of a six-letter word that had
an E, well, now he's going

3359
02:22:56,096 --> 02:22:58,596
to change his mind and
choose any other word

3360
02:22:58,596 --> 02:23:00,596
from the dictionary
that doesn't have any E.

3361
02:23:00,596 --> 02:23:02,086
So he can say, "Sorry, no E's."

3362
02:23:02,376 --> 02:23:05,356
If you then are kind of
smart in a sort of Wheel

3363
02:23:05,356 --> 02:23:07,856
of Fortune sense, you go with
A, another common letter.

3364
02:23:08,096 --> 02:23:10,516
Computer is going to figure
out, "Let me find a word

3365
02:23:10,516 --> 02:23:12,686
in the dictionary that
doesn't have E's or A's

3366
02:23:12,686 --> 02:23:14,556
so I can say no, no E's or A's."

3367
02:23:14,836 --> 02:23:18,286
So it's constantly dodging
your guesses in this way.

3368
02:23:18,466 --> 02:23:20,906
And it turns out that if you
give the user a finite number

3369
02:23:20,906 --> 02:23:22,876
of guesses like four
chances or five

3370
02:23:22,876 --> 02:23:25,656
or some relatively small number,
odds are they're not going

3371
02:23:25,656 --> 02:23:27,996
to win the game and they're
going to feet like an idiot

3372
02:23:27,996 --> 02:23:30,246
when they realize that the
six-letter word was something

3373
02:23:30,246 --> 02:23:32,736
so obvious but it
just didn't happen

3374
02:23:32,736 --> 02:23:34,086
to have the letters they chose.

3375
02:23:34,086 --> 02:23:37,446
And that's because the computer
isn't really changing its mind

3376
02:23:37,756 --> 02:23:40,846
try after try, it just has
a large dictionary of words

3377
02:23:41,006 --> 02:23:44,406
and it's constantly throwing
words away based on your guesses

3378
02:23:44,626 --> 02:23:47,136
until you effectively
paint it into a corner.

3379
02:23:47,476 --> 02:23:49,186
So you'll be implementing
this in the form

3380
02:23:49,186 --> 02:23:52,176
of a utility application
which is one of the templates

3381
02:23:52,556 --> 02:23:53,846
that was provided by Xcode.

3382
02:23:53,846 --> 02:23:55,956
It's nice in that it's
more complex than any

3383
02:23:55,956 --> 02:23:57,486
of these examples
we've done thus far,

3384
02:23:57,486 --> 02:23:58,936
and we'll see others
next week as well

3385
02:23:58,936 --> 02:24:00,096
as others in lab this week.

3386
02:24:00,406 --> 02:24:02,706
But the template
alone that comes

3387
02:24:02,706 --> 02:24:05,616
with Xcode gives you
this nice little effect.

3388
02:24:05,916 --> 02:24:07,776
If I click this UIButton
that looks

3389
02:24:07,776 --> 02:24:10,876
like a little information sign,
notice I get a two-sided UI.

3390
02:24:10,876 --> 02:24:13,256
And this is nice because
as you'll see in the spec,

3391
02:24:13,596 --> 02:24:16,266
you'll be challenged to
implement your user interface,

3392
02:24:16,266 --> 02:24:19,116
your UIViews in the
front of this interface.

3393
02:24:19,346 --> 02:24:21,106
But then there's going to be
some settings on the back,

3394
02:24:21,296 --> 02:24:23,486
some sliders for
instance that allow you

3395
02:24:23,486 --> 02:24:26,546
to change the word size to
make the game easier or harder,

3396
02:24:26,706 --> 02:24:28,916
and the number of guesses
that the user has allowed.

3397
02:24:28,916 --> 02:24:31,446
And then when you flip
it back by calling done,

3398
02:24:31,836 --> 02:24:34,716
you'll actually have a chance
then to play that game again.

3399
02:24:34,716 --> 02:24:37,126
This would be a chance
to play with NSArrays,

3400
02:24:37,216 --> 02:24:38,796
NSDictionaries potentially,

3401
02:24:38,996 --> 02:24:41,226
some of the building blocks
we've talked about this week

3402
02:24:41,286 --> 02:24:43,656
and last and we'll continue
talking about next week

3403
02:24:43,856 --> 02:24:47,116
so that ultimately, you will
begin this process by filling

3404
02:24:47,116 --> 02:24:48,056
in some of these blanks.

3405
02:24:48,436 --> 02:24:50,766
And so, what I would
encourage you to do between now

3406
02:24:50,766 --> 02:24:54,576
and Wednesday is to
actually try to one, go home,

3407
02:24:54,626 --> 02:24:56,976
download the source code from
tonight and actually tinker

3408
02:24:56,976 --> 02:24:58,816
with some of the examples,
delete lines of codes,

3409
02:24:58,846 --> 02:25:00,626
see what breaks and
try to understand why.

3410
02:25:00,926 --> 02:25:03,306
And I would also strongly
encourage everyone to take

3411
02:25:03,306 --> 02:25:06,556
at least one or two of tonight's
examples, maybe Nib1, Nib2,

3412
02:25:06,986 --> 02:25:08,616
and try to recreate it.

3413
02:25:08,726 --> 02:25:11,036
Even if you're kind of
cheating by comparing your code

3414
02:25:11,036 --> 02:25:13,176
against mine side by side,
but go through the motions

3415
02:25:13,466 --> 02:25:14,616
of like writing up the code,

3416
02:25:14,616 --> 02:25:17,216
figuring out how we wired things
together 'cause odds are they'll

3417
02:25:17,216 --> 02:25:19,266
take you half an hour
potentially 'cause you'll miss

3418
02:25:19,506 --> 02:25:22,126
some stupid little detail,
but it's that just kind

3419
02:25:22,126 --> 02:25:23,916
of muscle memory that
you want to build up so

3420
02:25:23,916 --> 02:25:26,166
that when you really want to
do things that are of interest

3421
02:25:26,166 --> 02:25:28,416
and sort of fun in terms
of this kind of game,

3422
02:25:28,606 --> 02:25:31,476
you won't get tripped up on
some of those little nuances.

3423
02:25:31,476 --> 02:25:33,106
And we'll spend more
time on that in lab.

3424
02:25:33,106 --> 02:25:35,556
We'll also do a walkthrough of
sorts to get you comfortable

3425
02:25:35,556 --> 02:25:38,776
with the spec and we'll dive in
much deeper next Monday as well.

3426
02:25:38,776 --> 02:25:40,426
So I'll stick around for
questions but otherwise,

3427
02:25:40,426 --> 02:25:41,156
why don't we call it a night.

3428
02:25:41,456 --> 02:25:42,946
See you next week.

3429
02:25:43,516 --> 02:26:10,830
[ Silence ]

