-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path1. Introduction to iOS 11, Xcode 9 and Swift 4.srt
8046 lines (6426 loc) · 166 KB
/
1. Introduction to iOS 11, Xcode 9 and Swift 4.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,202
[MUSIC]
2
00:00:04,271 --> 00:00:09,341
Stanford University.
>> Okay, well welcome then,
3
00:00:09,409 --> 00:00:13,545
to Stanford CS193P. This is
Developing Applications for
4
00:00:13,614 --> 00:00:15,914
iOS. Hopefully you're
in the right place.
5
00:00:15,983 --> 00:00:20,319
This is fall quarter of 2017.
And I'm gonna breeze through
6
00:00:20,387 --> 00:00:22,754
a few slides here. And
then I'm gonna sit down and
7
00:00:22,823 --> 00:00:25,256
do a nice involved demo just
to show you what it's like
8
00:00:25,325 --> 00:00:27,960
developing apps for iOS.
That's the best way to learn,
9
00:00:28,029 --> 00:00:30,094
it's for me to show
you how to do it. So
10
00:00:30,163 --> 00:00:32,231
this will be what I'm gonna
talk about briefly first.
11
00:00:32,299 --> 00:00:33,465
So what are you gonna
learn in this course?
12
00:00:33,534 --> 00:00:35,901
Of course you're gonna learn
how to build cool apps.
13
00:00:35,970 --> 00:00:39,304
iOS apps are cool.
Why are they cool?
14
00:00:39,372 --> 00:00:42,540
Well, they're cool because
they live in your pocket, you
15
00:00:42,609 --> 00:00:45,777
can show them to your friends
right there, also incredibly
16
00:00:45,845 --> 00:00:48,813
easy to sell and market your
apps online because you have
17
00:00:48,882 --> 00:00:52,017
the App Store. And there's a
very vibrant community that's
18
00:00:52,086 --> 00:00:54,953
because Apple is always doing
cool new things like virtual
19
00:00:55,021 --> 00:00:57,622
reality and machine learning
and all these things.
20
00:00:57,691 --> 00:01:00,725
And there are millions of apps
on the app store, so there's
21
00:01:00,794 --> 00:01:03,428
a lot of people doing this
stuff. But you're also gonna
22
00:01:03,497 --> 00:01:05,263
learn in this class
a little bit of real life
23
00:01:05,332 --> 00:01:06,965
Object-Oriented Programming.
24
00:01:07,034 --> 00:01:10,134
You all are taking classes
networking and databases and
25
00:01:10,203 --> 00:01:13,238
graphics, your other CS
classes. And here we're gonna
26
00:01:13,306 --> 00:01:15,807
do all those things in
a real life platform.
27
00:01:15,876 --> 00:01:19,177
Because over the weeks we
are going to do all of those
28
00:01:19,246 --> 00:01:22,480
computer science things for
real. And especially
29
00:01:22,549 --> 00:01:25,684
Object-Oriented Programming,
cuz iOS is completely and
30
00:01:25,753 --> 00:01:29,053
utterly object-oriented.
You cannot develop for
31
00:01:29,122 --> 00:01:32,357
iOS without being serious
object oriented programming.
32
00:01:32,426 --> 00:01:34,960
And that brings me to my
prerequisite for this course,
33
00:01:35,029 --> 00:01:37,496
which is Object-Oriented
Programming. You have to be
34
00:01:37,564 --> 00:01:39,631
comfortable with
Object-Oriented Programming.
35
00:01:39,699 --> 00:01:42,835
Definitely CS106 A and
B and hopefully CS108,
36
00:01:42,903 --> 00:01:45,704
which is Object-Oriented
Programming here at
37
00:01:45,773 --> 00:01:48,573
Stanford. And the other thing
you gotta to be comfortable
38
00:01:48,641 --> 00:01:51,343
with is writing a lot of code.
All your homework in this
39
00:01:51,412 --> 00:01:53,912
class, there's a little bit of
reading the first few weeks
40
00:01:53,981 --> 00:01:56,047
about the language that
we're gonna develop in.
41
00:01:56,116 --> 00:01:58,082
But mostly it's just
programming, programming and
42
00:01:58,151 --> 00:02:01,320
more programming. So if you're
not comfortable writing a lot
43
00:02:01,388 --> 00:02:04,289
of code then this class,
you might wanna wait until
44
00:02:04,358 --> 00:02:07,059
you've taken some other
classes that ask you to
45
00:02:07,127 --> 00:02:09,962
write a lot of code
before you dive in here.
46
00:02:10,030 --> 00:02:13,164
So what's in iOS? What am
I gonna be teaching you?
47
00:02:13,233 --> 00:02:15,533
I've divided it here
into four layers.
48
00:02:15,602 --> 00:02:18,003
The bottom layer is
near the hardware, and
49
00:02:18,071 --> 00:02:21,840
the top layer is near the
user. So at that bottom layer,
50
00:02:21,909 --> 00:02:25,710
you might be surprised to
find out that iOS is Unix.
51
00:02:25,779 --> 00:02:29,614
It's a BSD variant of Unix.
And so all of the stuff
52
00:02:29,683 --> 00:02:34,453
that's going on down there is
all happening in C basically.
53
00:02:34,521 --> 00:02:38,156
Unix is mostly written in C,
most of that stuff is in C.
54
00:02:38,225 --> 00:02:41,460
I'm not gonna teach you
anything at this layer.
55
00:02:41,528 --> 00:02:42,760
This is
an object-oriented class,
56
00:02:42,829 --> 00:02:44,396
everything we do will
be object-oriented.
57
00:02:44,465 --> 00:02:47,432
You will not see me teaching
you anything down there,
58
00:02:47,501 --> 00:02:49,234
The next layer up is called
the Core Services layer.
59
00:02:49,235 --> 00:02:50,968
all right?
60
00:02:51,037 --> 00:02:54,939
This is an object-oriented
layer on top of those lower
61
00:02:55,008 --> 00:02:59,377
services. This I will be
teaching you. So if we wanna
62
00:02:59,446 --> 00:03:02,547
do things like find out where
the phone is on the planet or
63
00:03:02,616 --> 00:03:05,817
find out its orientation or
access some files in the file
64
00:03:05,886 --> 00:03:09,320
system, we're gonna be using
this layer right here and I'll
65
00:03:09,389 --> 00:03:13,191
be teaching you all about this
layer. Core Services layer.
66
00:03:13,259 --> 00:03:17,428
The next layer up, this is
kind of not a strict layering,
67
00:03:17,497 --> 00:03:20,665
but is the Media layer.
Don't forget that your iPhone
68
00:03:20,733 --> 00:03:24,369
is essentially, originally
an iPod with a phone in it. So
69
00:03:24,438 --> 00:03:27,739
it's got all kinds of media,
video, audio,
70
00:03:27,807 --> 00:03:31,543
many different kinds,
images, all this stuff. And
71
00:03:31,611 --> 00:03:34,446
unfortunately this is a layer
I'd love to teach you but
72
00:03:34,515 --> 00:03:37,482
I have to cut something, so
I'm not gonna talk much about
73
00:03:37,550 --> 00:03:40,718
this layer, unfortunately.
So I know some of you might be
74
00:03:40,787 --> 00:03:43,354
wanting to write a cool game
that has 3D sound with TIE
75
00:03:43,423 --> 00:03:46,224
fighters ripping around from
the back of you to the front.
76
00:03:46,292 --> 00:03:49,060
And that's all possible, and
quite straightforward to do,
77
00:03:49,128 --> 00:03:51,629
but in ten weeks I have to
pick what I'm gonna teach you,
78
00:03:51,698 --> 00:03:54,832
and so I'm gonna focus on Core
Services. And then this layer,
79
00:03:54,901 --> 00:03:58,803
which is Cocoa Touch. So Cocoa
Touch is the UI layer of iOS.
80
00:03:58,872 --> 00:04:00,305
This is where the buttons and
81
00:04:00,374 --> 00:04:02,507
the sliders,
all those things are in. And
82
00:04:02,576 --> 00:04:05,943
there's also, really powerful
objects here, like the Map Kit
83
00:04:06,012 --> 00:04:08,679
has a single object that you
can just drop in your app
84
00:04:08,748 --> 00:04:11,583
that gives you pretty much
the entire functionality of
85
00:04:11,652 --> 00:04:15,053
the map's app inside a
rectangle, inside your window.
86
00:04:15,121 --> 00:04:18,256
So, you're talking about a
wide variety of UI stuff here
87
00:04:18,324 --> 00:04:21,660
and I'm gonna try and cover
as much of this as I can. So
88
00:04:21,728 --> 00:04:23,929
that's what you're gonna
learn in this class.
89
00:04:23,997 --> 00:04:26,965
The platform we're gonna
develop on is Xcode 9, so
90
00:04:27,034 --> 00:04:29,568
you're all gonna need
to download Xcode 9,
91
00:04:29,637 --> 00:04:32,471
it's free and
it only runs on your Mac.
92
00:04:32,539 --> 00:04:35,140
And, there's this other little
app, Instruments, but it's
93
00:04:35,208 --> 00:04:37,776
really kind of just an add-on
to Xcode 9. We're gonna do
94
00:04:37,844 --> 00:04:41,212
everything in Xcode 9, source
code, editing, debugging,
95
00:04:41,281 --> 00:04:44,515
all that stuff is gonna happen
in Xcode 9. You're also gonna
96
00:04:44,584 --> 00:04:47,986
have to learn a new language.
iOS has two languages you can
97
00:04:48,054 --> 00:04:50,154
develop in,
Objective-C and Swift.
98
00:04:50,223 --> 00:04:53,291
Swift is the newer one, that's
the one I'm gonna teach you.
99
00:04:53,360 --> 00:04:56,327
Everything you learn though
in Swift, all about iOS,
100
00:04:56,396 --> 00:04:59,264
will apply if you later wanna
go learn Objective-C and
101
00:04:59,332 --> 00:05:02,367
work for a company that is
still writing in Objective-C,
102
00:05:02,436 --> 00:05:05,036
which is perfectly valid
language to write in.
103
00:05:05,105 --> 00:05:06,838
But Swift kind of
a cool new language,
104
00:05:06,906 --> 00:05:09,107
I think you're really,
really gonna like it.
105
00:05:09,176 --> 00:05:12,310
If you're a serious Computer
Scientist another language is
106
00:05:12,378 --> 00:05:15,513
like big yawn whatever. Just
tell me what the syntax is,
107
00:05:15,582 --> 00:05:18,316
tell me what they key
fundamental mechanisms for
108
00:05:18,385 --> 00:05:21,319
designing with it are,
and I'll learn it.
109
00:05:21,388 --> 00:05:24,221
So, if you don't have that
attitude towards languages,
110
00:05:24,290 --> 00:05:27,158
you're gonna be kind of having
trouble out in the real world
111
00:05:27,227 --> 00:05:29,161
being a programmer.
And then of course,
112
00:05:29,229 --> 00:05:31,596
there's millions of what
we call Frameworks in iOS.
113
00:05:31,664 --> 00:05:33,331
Frameworks are just
collections of objects.
114
00:05:33,399 --> 00:05:36,234
Like the biggest one is UIKit,
that's where buttons, and
115
00:05:36,303 --> 00:05:38,769
sliders, and all that stuff
is. Foundation is another
116
00:05:38,838 --> 00:05:41,640
big one. That's where a lot of
that core services stuff I was
117
00:05:41,708 --> 00:05:44,141
talking about is. But there's
the Map Kit, Core Motion,
118
00:05:44,210 --> 00:05:46,478
Core Data, Object Orient,
Database, all kinds of them.
119
00:05:46,547 --> 00:05:49,513
And I'll try and get to as
many of them as I can. And
120
00:05:49,582 --> 00:05:53,652
last but definitely not least,
there is a Design Strategy for
121
00:05:53,721 --> 00:05:56,754
building iOS apps
that you have to use.
122
00:05:56,823 --> 00:05:59,290
This is not like an optional,
here's a good idea, why don't
123
00:05:59,359 --> 00:06:03,395
you design this way? You must
design this way. It's called
124
00:06:03,463 --> 00:06:06,331
MVC, Model View Controller.
How many people have,
125
00:06:06,399 --> 00:06:09,634
some experience doing MVC in
any other? Okay, it's not so
126
00:06:09,703 --> 00:06:12,303
many this quarter. Usually I
get half the people but
127
00:06:12,372 --> 00:06:14,439
I'll be teaching
all about MVC.
128
00:06:14,507 --> 00:06:17,342
The start of electron
Wednesday is gonna be a full
129
00:06:17,411 --> 00:06:21,345
coverage of MVC. What it is,
how it works, all that stuff,
130
00:06:21,414 --> 00:06:25,584
all right? So now, I'm gonna
dive right into a big demo
131
00:06:25,652 --> 00:06:29,921
that's the best way to learn
how to do iOS development is
132
00:06:29,990 --> 00:06:32,557
with a demo. Seeing it happen,
we're gonna build
133
00:06:32,625 --> 00:06:34,926
an application from scratch.
This slide right here,
134
00:06:34,995 --> 00:06:36,628
is a slide for
you to look at later, and
135
00:06:36,696 --> 00:06:39,197
see, did I learn all these
things, because I should have
136
00:06:39,266 --> 00:06:43,468
learned them today. This
is not a slide to read now.
137
00:06:43,536 --> 00:06:45,503
Since I'm not gonna get
back to the slides,
138
00:06:45,572 --> 00:06:48,139
let me just say what's coming
up real quick. You are gonna
139
00:06:48,208 --> 00:06:50,008
have a reading assignment
that goes out today.
140
00:06:50,077 --> 00:06:53,044
It's basically starting
to read the manual on
141
00:06:53,113 --> 00:06:55,780
Swift. So you can learn this
new language. It'll be all
142
00:06:55,849 --> 00:06:58,950
spreaded out over three maybe
four weeks, so you don't have
143
00:06:59,019 --> 00:07:00,918
too much reading all at once.
>> But
144
00:07:00,987 --> 00:07:03,588
those reading assignments
are gonna be in addition to
145
00:07:03,657 --> 00:07:06,224
programming assignments. The
reading assignments are going
146
00:07:06,293 --> 00:07:07,792
to go out on Monday,
they come back on Monday,
147
00:07:07,861 --> 00:07:08,993
they're due the next Monday.
And
148
00:07:09,062 --> 00:07:11,029
then the program assignments
go out on Wednesday and
149
00:07:11,098 --> 00:07:12,997
then they're due the next
Wednesday generally.
150
00:07:13,066 --> 00:07:15,599
Okay, at least we'll start
the quarter that way.
151
00:07:15,668 --> 00:07:17,636
On Friday,
we have an optional section,
152
00:07:17,704 --> 00:07:20,138
it means optional that you
don't have to go there.
153
00:07:20,206 --> 00:07:22,841
The topics we're gonna cover
are kind of additional, but
154
00:07:22,910 --> 00:07:24,943
this Friday is one is
a big one, it's tips and
155
00:07:25,011 --> 00:07:27,412
tricks of Xcode, including
how to use the debugger.
156
00:07:27,481 --> 00:07:29,747
So if you've never used
the debugger in Xcode,
157
00:07:29,816 --> 00:07:31,115
this is a good one to go to,
so
158
00:07:31,184 --> 00:07:33,685
you can see how to use
the debugger for your problem.
159
00:07:33,754 --> 00:07:36,220
If not, you know, it's
a debugger, if you used other
160
00:07:36,289 --> 00:07:39,356
debuggers, you could probably
figure it out, but anyway, it
161
00:07:39,425 --> 00:07:42,427
says right there that it's in
Hewlett 205, but I don't think
162
00:07:42,495 --> 00:07:44,962
there's such a room. I think
it might be in Hewlett 105.
163
00:07:45,031 --> 00:07:47,532
I don't know, watch Piazza and
we'll tell you where it is.
164
00:07:47,601 --> 00:07:51,869
But it is gonna be 11:30 to
12:20. And so next week we'll
165
00:07:51,938 --> 00:07:55,473
talk more about Swift and
then launch into all of iOS,
166
00:07:55,541 --> 00:07:59,243
all right? So let's jump
right into the demo here.
167
00:07:59,312 --> 00:08:02,447
And I apologize in advance for
going fast on this demo,
168
00:08:02,516 --> 00:08:03,848
because we have
a lot to cover, and
169
00:08:03,916 --> 00:08:05,650
you're gonna find that in
this course, in general,
170
00:08:05,719 --> 00:08:08,153
I tend to go pretty quick, cuz
I wanna teach you as much of
171
00:08:08,222 --> 00:08:13,057
this stuff as I possibly can.
All right,
172
00:08:13,126 --> 00:08:16,728
Xcode. Here's Xcode.
I went to the Mac App Store.
173
00:08:16,797 --> 00:08:19,030
I searched Xcode, I found it.
I downloaded it.
174
00:08:19,099 --> 00:08:21,866
It was free, it doesn't cost
me a dime to develop for
175
00:08:21,935 --> 00:08:24,602
iOS. When you launch Xcode for
the first time,
176
00:08:24,671 --> 00:08:26,637
you're gonna get this
thing right here. So
177
00:08:26,706 --> 00:08:29,340
we need to build an app. Now,
I decided to show you the app
178
00:08:29,408 --> 00:08:32,844
that I'm gonna build here in
real life. So you see these
179
00:08:32,912 --> 00:08:36,681
cards that I've very artfully
taped up here? These cards
180
00:08:36,750 --> 00:08:39,584
are going to let me play
a game called Concentration.
181
00:08:39,653 --> 00:08:42,654
How many people have heard
of the game Concentration?
182
00:08:42,722 --> 00:08:45,389
Not too many. Okay, so
Concentration is just a card
183
00:08:45,458 --> 00:08:48,059
game. Behind these cards
that are all face down,
184
00:08:48,127 --> 00:08:50,395
there are some pictures,
and the goal is for
185
00:08:50,463 --> 00:08:53,464
you to match the pictures up.
So there's 12 cards,
186
00:08:53,533 --> 00:08:56,834
6 pairs of pictures, so I get
to pick two cards at a time,
187
00:08:56,903 --> 00:08:58,602
and if they match, I win, and
188
00:08:58,671 --> 00:09:01,138
the cards go away.
If they don't match,
189
00:09:01,207 --> 00:09:04,008
I have to turn them back down
and pick two other cards, and
190
00:09:04,077 --> 00:09:06,778
it's called Concentration
cuz I have to concentrate on
191
00:09:06,847 --> 00:09:09,681
the ones that didn't match, so
that later I can go back and
192
00:09:09,750 --> 00:09:11,783
match them. So, let's just
flip some cards over here.
193
00:09:11,852 --> 00:09:13,984
I'm gonna start with
this one right here.
194
00:09:14,053 --> 00:09:15,687
I really don't know what's
behind there, but let's see.
195
00:09:15,756 --> 00:09:20,491
Okay, this one is
a pair of purple bats.
196
00:09:20,560 --> 00:09:22,460
So now I'm trying to find
another pair of purple bats,
197
00:09:22,529 --> 00:09:25,529
let's try right here.
I found them right away, okay.
198
00:09:25,598 --> 00:09:27,699
So this is a match,
I get some points.
199
00:09:27,767 --> 00:09:31,535
These come off the board.
So now I'm searching for
200
00:09:31,604 --> 00:09:35,240
more matches. All right, let's
try this one right here. It's
201
00:09:35,308 --> 00:09:39,977
a ghost. I got the ghost right
there. Let's try this one.