-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy path4. More Swift.srt
8192 lines (6541 loc) · 169 KB
/
4. More Swift.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,736
[MUSIC]
2
00:00:04,804 --> 00:00:08,840
Stanford University.
>> All right,
3
00:00:08,909 --> 00:00:11,743
well, welcome to
Lecture number four?
4
00:00:11,812 --> 00:00:15,180
No. Yes, four of
Stanford CS193P, Fall of 2017.
5
00:00:15,249 --> 00:00:18,583
Today, we're gonna continue
to talk about Swift.
6
00:00:18,652 --> 00:00:21,019
This will be the last we talk
about Swift. After this,
7
00:00:21,088 --> 00:00:23,655
we're pretty much gonna
kinda assume you know Swift.
8
00:00:23,724 --> 00:00:26,491
Obviously you'll be learning
on the fly as you go, but
9
00:00:26,560 --> 00:00:27,592
starting next week,
10
00:00:27,661 --> 00:00:29,861
we're gonna dive right
into iOS, UI kit and
11
00:00:29,930 --> 00:00:33,198
all that. So I'm gonna do the
quick demo that I postponed
12
00:00:33,267 --> 00:00:36,968
from Wednesday of this
mutating keyword that we have
13
00:00:37,037 --> 00:00:39,771
to add when we make something
a struct versus a class. Then
14
00:00:39,840 --> 00:00:43,274
we're gonna talk about two
very important topics today.
15
00:00:43,343 --> 00:00:45,910
Protocols and closures.
16
00:00:45,979 --> 00:00:48,647
Functions as types of general
those two and in between, I'm
17
00:00:48,715 --> 00:00:52,384
gonna talk about string which
is important in a way but
18
00:00:52,452 --> 00:00:55,553
it's not like the monstrous
important topics like
19
00:00:55,622 --> 00:01:00,859
prototypes and closures are.
So start right into that demo
20
00:01:00,928 --> 00:01:05,096
that I had. And remember, we
were talking at the time about
21
00:01:05,165 --> 00:01:09,400
making things a struct and
that it's a little different
22
00:01:09,469 --> 00:01:13,172
then a class because that copy
on write. Swift needs to know
23
00:01:13,240 --> 00:01:15,674
when a function might
actually modify the thing so
24
00:01:15,743 --> 00:01:18,576
that it knows to actually
make a real copy. So
25
00:01:18,645 --> 00:01:22,147
let's go back here to our app,
Concentration, where we were.
26
00:01:22,216 --> 00:01:26,751
Let's go to Concentration,
which if you recall,
27
00:01:26,820 --> 00:01:30,422
we made be a class.
A class Concentration. And
28
00:01:30,491 --> 00:01:32,857
this could have just as
easily been a struct.
29
00:01:32,926 --> 00:01:35,326
I really made it a class
just so when we're doing
30
00:01:35,395 --> 00:01:37,428
the initialize and stuff,
you could see the difference
31
00:01:37,497 --> 00:01:40,331
between Concentration and card
because card was a struct and
32
00:01:40,400 --> 00:01:42,267
Concentration was a class.
But there's
33
00:01:42,336 --> 00:01:44,136
no reason Concentration
wouldn't have been a struct.
34
00:01:44,204 --> 00:01:47,572
It's probably, it might even
be better as a struct here.
35
00:01:47,641 --> 00:01:51,376
Now we don't pass
Concentration games around and
36
00:01:51,445 --> 00:01:55,113
all that, it just kinda
sits in our controller.
37
00:01:55,182 --> 00:01:58,716
Not really a pointer to our
model but this would embed
38
00:01:58,785 --> 00:02:01,320
the model there. So it doesn't
really matter too much.
39
00:02:01,388 --> 00:02:06,124
But when I change this from
class to struct, notice here
40
00:02:06,193 --> 00:02:09,194
that I get an error. And let's
scroll down and here they are,
41
00:02:09,263 --> 00:02:11,363
here's the error and
this error is saying,
42
00:02:11,431 --> 00:02:15,533
cannot assign to property,
self is immutable. Why is this
43
00:02:15,602 --> 00:02:18,970
saying self is immutable? I'm
trying to change the card here
44
00:02:19,039 --> 00:02:21,273
to say that they've match,
for example.
45
00:02:21,342 --> 00:02:24,442
Well, self is immutable
because this function right
46
00:02:24,511 --> 00:02:29,514
is not marked mutable, and so
it is assumed to not mutate,
47
00:02:29,583 --> 00:02:34,152
not change this
Concentration object. So
48
00:02:34,220 --> 00:02:37,722
of course we need chooseCard
to change the Concentration
49
00:02:37,791 --> 00:02:40,325
object cuz it's the main
thing. The change of the game,
50
00:02:40,394 --> 00:02:43,795
so all we need to do is add
mutable right here, that
51
00:02:43,864 --> 00:02:47,932
marks, sorry, mutating, not
mutable, you add that here and
52
00:02:48,001 --> 00:02:51,470
that says that this is
a mutating function,
53
00:02:51,538 --> 00:02:54,372
all the errors go away,
and we're all good to go.
54
00:02:54,441 --> 00:02:59,544
Question?
>> [INAUDIBLE]
55
00:02:59,613 --> 00:03:00,479
>> Okay, the question is why
56
00:03:00,547 --> 00:03:01,680
does this not need it?
57
00:03:01,749 --> 00:03:05,784
This is a var. And
this var is get and set, so
58
00:03:05,852 --> 00:03:10,121
Swift already knows this is
mutating, cuz it's settable.
59
00:03:10,190 --> 00:03:12,357
If this was get only, then
it wouldn't be mutating and
60
00:03:12,426 --> 00:03:16,028
Swift would know that.
So for vars, Swift knows. And
61
00:03:16,096 --> 00:03:19,264
if it's not a computed var,
like look at this var,
62
00:03:19,332 --> 00:03:22,701
how does this one know? That's
because, well, this one is
63
00:03:22,770 --> 00:03:26,871
read only externally but
for us it's writable. Here,
64
00:03:26,940 --> 00:03:29,641
anything that's a var it
assumed it's writable. If it
65
00:03:29,709 --> 00:03:34,846
were a let, a let, then it
would think it's not writable.
66
00:03:34,914 --> 00:03:39,450
So that's how you do mutating,
or mutable. If for vars is
67
00:03:39,519 --> 00:03:42,620
whether they're vars or let,
or for computer property,
68
00:03:42,689 --> 00:03:47,192
whether it has both get and
set. Where we got all that? So
69
00:03:47,260 --> 00:03:49,060
it's only funcs that we
have to put that on.
70
00:03:49,129 --> 00:03:52,297
Yeah, question.
>> [INAUDIBLE]
71
00:03:52,366 --> 00:03:52,931
>> Yes, so the question is,
72
00:03:52,999 --> 00:03:55,733
why is this you need to a
struct and not a class, why do
73
00:03:55,802 --> 00:03:58,002
I need to struct? And that's
very important to understand,
74
00:03:58,071 --> 00:04:02,974
very good question. Structs
are value types, not reference
75
00:04:03,043 --> 00:04:05,844
types. So structs don't live
in the heap. As we pass
76
00:04:05,913 --> 00:04:09,013
the struct around since it's
a value type, it gets copied.
77
00:04:09,082 --> 00:04:11,649
Constantly copied everywhere.
Pass through a function,
78
00:04:11,718 --> 00:04:14,486
a copy is made. Put in a var,
a copy is made. Well,
79
00:04:14,554 --> 00:04:17,222
soon, that would be incredibly
inefficient if it actually
80
00:04:17,291 --> 00:04:21,159
copy the bits of this entire
thing every time. So instead,
81
00:04:21,228 --> 00:04:24,028
Swift is smart. It only
actually copies the bits when
82
00:04:24,097 --> 00:04:27,799
you mutate it. That's called
copy-on-write semantics.
83
00:04:27,867 --> 00:04:29,434
This is why structs
are different,
84
00:04:29,503 --> 00:04:31,903
because they have that
copy-on-write semantics.
85
00:04:31,972 --> 00:04:35,106
A class doesn't have that, cuz
when you pass a class around,
86
00:04:35,175 --> 00:04:37,075
the class just lives
once in the heap,
87
00:04:37,144 --> 00:04:38,810
and you're passing
pointers to it around.
88
00:04:38,878 --> 00:04:41,213
And there could be 20
pointers to the same object.
89
00:04:41,281 --> 00:04:44,683
Totally different kinda model
for memory. So that's why,
90
00:04:44,751 --> 00:04:48,719
it's because it's a value type
that copy on writes stuff,
91
00:04:48,788 --> 00:04:51,523
that's why we care
that it's mutating.
92
00:04:51,592 --> 00:04:56,828
Let's go back to our slides
and talk about the first very,
93
00:04:56,897 --> 00:05:01,900
very important topic
today which is protocols.
94
00:05:01,968 --> 00:05:05,737
It's the fourth pillar of
building data structures
95
00:05:05,805 --> 00:05:09,307
inside of Swift. You already
know a lot now about class,
96
00:05:09,375 --> 00:05:14,679
struct and enum. And
a protocol is something where
97
00:05:14,748 --> 00:05:18,382
its a very simple concept.
A protocol is basically just
98
00:05:18,451 --> 00:05:21,453
a list of methods and vars
with no implementation. But
99
00:05:21,522 --> 00:05:23,722
it's the use of having
protocols. It's so
100
00:05:23,790 --> 00:05:27,025
pervasively used throughout
the Swift language and
101
00:05:27,093 --> 00:05:30,529
the runtime that makes it so
powerful. So first I'm just
102
00:05:30,597 --> 00:05:32,664
gonna go through a little bit
of why we have protocols.
103
00:05:32,733 --> 00:05:35,133
Then I'll show you
the syntax of protocols.
104
00:05:35,201 --> 00:05:37,635
What it looks like to type in
a protocol and define one.
105
00:05:37,704 --> 00:05:40,005
Then we'll start talking
about the use of protocols.
106
00:05:40,073 --> 00:05:43,674
Where do we use them?
Why are they so valuable? So
107
00:05:43,743 --> 00:05:47,178
let's start with,
what protocols are all about?
108
00:05:47,247 --> 00:05:51,616
Protocols are essentially
a way for
109
00:05:51,685 --> 00:05:55,219
callers to call an API
with anything they want.
110
00:05:55,288 --> 00:05:57,122
Any struct, enum, class,
111
00:05:57,190 --> 00:06:00,391
it can pass anything they
want in. And at the same time
112
00:06:00,460 --> 00:06:04,596
the receiving method can
specify what it really wants.
113
00:06:04,665 --> 00:06:08,132
So both sides get to do what
they want. The receiving thing
114
00:06:08,201 --> 00:06:10,868
gets to say what it wants
the thing you're passing and
115
00:06:10,937 --> 00:06:13,772
to do and the thing passing in
can pass whatever it wants as
116
00:06:13,840 --> 00:06:17,409
long as it does that thing.
And to do all this all
117
00:06:17,478 --> 00:06:22,380
a protocol is, is a list
of vars and functions.
118
00:06:22,449 --> 00:06:24,649
That's all a protocol is.
It's just a list of them.
119
00:06:24,718 --> 00:06:27,251
Not an implementation or
anything, just a list of them.
120
00:06:27,320 --> 00:06:30,622
And it's how we use them in
the API that let's us get this
121
00:06:30,691 --> 00:06:33,558
behavior of having
the callers and the callees.
122
00:06:33,626 --> 00:06:37,028
Get to express things
the way they want. Now, what
123
00:06:37,096 --> 00:06:40,532
are protocols good for? They
make APIs very flexible and
124
00:06:40,600 --> 00:06:42,500
expressive, as
you're going to see.
125
00:06:42,569 --> 00:06:45,870
They're super good for blind
structured communication.
126
00:06:45,939 --> 00:06:48,439
Like, remember back
to my MVC talk.
127
00:06:48,508 --> 00:06:50,542
The communication between
the view and the controller.
128
00:06:50,611 --> 00:06:54,011
All those will, did, shoulds.
And the data at count,
129
00:06:54,080 --> 00:06:56,982
those kind of communications
between a view controller.
130
00:06:57,050 --> 00:06:59,784
That all had to be blind,
because the views are generic
131
00:06:59,853 --> 00:07:03,721
and the controllers are very
specific, and protocols is how
132
00:07:03,790 --> 00:07:07,792
we make that work. It's also
great for mandating behavior,
133
00:07:07,861 --> 00:07:11,529
for example, in a dictionary,
a dictionary's a hash table,
134
00:07:11,597 --> 00:07:15,032
raise your hand if you know
what a hash table is when I
135
00:07:15,101 --> 00:07:19,037
say that. Okay, everybody.
Good. So it's a hash table.
136
00:07:19,105 --> 00:07:22,407
So the keys of a dictionary
clearly have to be hashable.
137
00:07:22,476 --> 00:07:25,509
You have to be able to get a
hash value otherwise you can't
138
00:07:25,578 --> 00:07:28,580
hash them and put them in
as keys of this hash table.
139
00:07:28,649 --> 00:07:31,983
Well, protocols let us define
dictionary in a way so
140
00:07:32,052 --> 00:07:35,319
that we don't use any keys
that aren't hashable.
141
00:07:35,388 --> 00:07:38,056
So mandating behavior
passibility, for example,
142
00:07:38,125 --> 00:07:40,992
of the key it's great for
that. It's also great for
143
00:07:41,061 --> 00:07:43,994
sharing functionality
between very disparate types,
144
00:07:44,063 --> 00:07:47,064
types that you would never use
object oriented inheritance to
145
00:07:47,133 --> 00:07:50,501
make them share a base class,
but they are very similar, for
146
00:07:50,570 --> 00:07:54,472
example, string, array,
countable range. They are all
147
00:07:54,540 --> 00:07:57,475
collections of things. Now
they're completely different.
148
00:07:57,544 --> 00:07:58,943
Come one,
a countable range and
149
00:07:59,012 --> 00:08:01,446
a string are completely
different. But they are both
150
00:08:01,515 --> 00:08:04,182
collections, a string is
a collection of characters,
151
00:08:04,251 --> 00:08:06,584
accountable range is a
collection of integers if it's
152
00:08:06,653 --> 00:08:09,487
an accountable range of int.
So they do share some and
153
00:08:09,555 --> 00:08:12,624
protocols has a mechanism to
allow you to share that kind
154
00:08:12,693 --> 00:08:15,426
of similarity without having
to have all those thing
155
00:08:15,495 --> 00:08:18,763
inherit from some kind common
base class that knows about
156
00:08:18,832 --> 00:08:21,666
collections of things. You
see what I'm saying? And so
157
00:08:21,735 --> 00:08:25,536
in a way, protocols provide
multiple inheritance. Now,
158
00:08:25,605 --> 00:08:28,873
since protocols are only doing
the declaration of the vars
159
00:08:28,942 --> 00:08:32,277
and functions, there's no
storage of, of those things,
160
00:08:32,345 --> 00:08:34,378
so there's no
inheritance of data,
161
00:08:34,447 --> 00:08:36,447
it's purely just
inheritance of,
162
00:08:36,516 --> 00:08:41,152
inheritance of functionality.
All right, as I go
163
00:08:41,221 --> 00:08:43,688
through this explanation, it's
super important to understand
164
00:08:43,757 --> 00:08:47,258
that protocols are just
a type. Just like a class is
165
00:08:47,327 --> 00:08:50,194
a type, a struct, an enum,
a protocol is a type.
166
00:08:50,263 --> 00:08:52,730
A first-class type, just like
all the rest of these types,
167
00:08:52,799 --> 00:08:54,398
that's very important
to understand.
168
00:08:54,467 --> 00:08:58,103
All right, so let's dive
into what a protocol is.
169
00:08:58,171 --> 00:08:59,804
There's three parts
to a protocol.
170
00:08:59,873 --> 00:09:02,974
One, there's the declaration
of the protocol, just like you
171
00:09:03,043 --> 00:09:05,443
have a declaration of a class
or an enum or a struct. For
172
00:09:05,511 --> 00:09:07,245
a protocol,
that's just a list of
173
00:09:07,313 --> 00:09:09,781
methods with their arguments
and return values and
174
00:09:09,850 --> 00:09:13,284
a list of vars. That's it,
that's what a protocol is,
175
00:09:13,353 --> 00:09:16,620
it's a declaration. Second
part of a protocol though
176
00:09:16,689 --> 00:09:21,259
is a class or a struct or
an enum raises its hand and
177
00:09:21,327 --> 00:09:24,628
claims to implement those
methods in vars in that
178
00:09:24,697 --> 00:09:27,832
protocol. So there's the claim
to implement a protocol,
179
00:09:27,901 --> 00:09:30,701
that's the second part.
Cuz somebody has to actually
180
00:09:30,770 --> 00:09:33,437
implement those methods that
the protocol is a list of, and
181
00:09:33,506 --> 00:09:36,274
that's classes, structs and
enums, so they have to claim.
182
00:09:36,343 --> 00:09:39,377
And then the third part is
the code in those classes,
183
00:09:39,446 --> 00:09:42,246
structs and in in enums
that actually implements
184
00:09:42,315 --> 00:09:45,115
the protocol methods in vars.
Okay, so
185
00:09:45,184 --> 00:09:47,852
notice I'm mentioning vars.
The vars,
186
00:09:47,921 --> 00:09:51,289
the storage of the vars,
if they're not computed vars,
187
00:09:51,358 --> 00:09:54,592
has to be in some struct,
enum or a class. Because,
188
00:09:54,661 --> 00:09:58,596
that's the only places you can
have any storage, all right?
189
00:09:58,665 --> 00:10:00,364
Okay, so that's it, those are
the three parts of a protocol.
190
00:10:00,433 --> 00:10:03,401
Now I'm gonna take
a little aside, and
191
00:10:03,470 --> 00:10:07,037
not even gonna spend too
much time on this, but
192
00:10:07,106 --> 00:10:11,209
all the methods and vars in
a protocol are mandatory.
193
00:10:11,278 --> 00:10:12,810
If you're wanna
raise your hand and
194
00:10:12,879 --> 00:10:14,378
say, I implement
this protocol.
195
00:10:14,447 --> 00:10:18,015
If you're a struct or enum or
a class, you have to implement
196
00:10:18,084 --> 00:10:22,187
all of the methods and all
the vars in Swift. However,
197
00:10:22,255 --> 00:10:27,024
in Objective-C, that was
not true. In Objective-C,
198
00:10:27,093 --> 00:10:31,762
protocols could have
optional methods. Methods or
199
00:10:31,831 --> 00:10:34,432
vars in the protocol that you
could choose to implement or
200
00:10:34,500 --> 00:10:38,970
not. Now, that's quite a big
difference and the way Swift
201
00:10:39,039 --> 00:10:42,306
deals with that is it allows
you to put @objc, in front of
202
00:10:42,375 --> 00:10:45,276