-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path4. More Swift.srt
7997 lines (6346 loc) · 206 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
本字幕由志愿者义务贡献,采用许可协议
知识共享 署名-非商业性使用-相同方式共享 3.0 美国
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.
2017 年秋季学期斯坦福 CS193P 的第四节课
5
00:00:15,249 --> 00:00:18,583
Today, we're gonna continue to talk about Swift.
今天我们将继续谈谈关于 Swift 的一些内容
6
00:00:18,652 --> 00:00:21,019
This will be the last we talk about Swift. After this,
这会是最后一节讲 Swift 的课程。在本节课之后
7
00:00:21,088 --> 00:00:23,655
we're pretty much gonna kinda assume you know Swift.
我会默认你们已经掌握 Swift 了
8
00:00:23,724 --> 00:00:26,491
Obviously you'll be learning on the fly as you go, but
显然之后你们也会学到更多 Swift 的内容
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
我们将会专注于 iOS、UIKit 和其他 iOS 开发
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
要使用的 mutating 关键字的例子
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 I had remembered, 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
因为它是写时复制(copy on write)的
23
00:01:13,240 --> 00:01:15,674
when a function might actually modify the thing so
Swift 需要知道一个函数何时会改变变量的值
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,
来到我们的 Concentration,如果你还记得
27
00:01:26,820 --> 00:01:30,422
we made be a class. A class Concentration. And
我们写了一个类,叫做 Concentration
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
Concentration 和 Card 的不同,因为 Card 是结构体
32
00:01:40,400 --> 00:01:42,267
Concentration was a class. But there's
而 Concentration 是一个类,但也没有什么
33
00:01:42,336 --> 00:01:44,136
no reason Concentration wouldn't have been a struct.
理由不让 Concentration 成为一个结构体
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
不能给属性赋值,self 是不可改变的。 为什么
43
00:02:15,602 --> 00:02:18,970
saying self is immutable? I'm trying to change the card here
它说 self 是不可改变的?我在尝试改变 card 的值
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
self 之所以是不可改变的,是因为这个函数
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
不会改变这个 Concentration 对象
48
00:02:34,220 --> 00:02:37,722
of course we need chooseCard to change the Concentration
当然我们需要这个函数改变 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
所以我们只需要在这里加上 mutable
51
00:02:43,864 --> 00:02:47,932
marks, sorry, mutating, not mutable, you add that here and
抱歉,不是 mutable,应该是 mutating,将它加在这里
52
00:02:48,001 --> 00:02:51,470
that says that this is a mutating function,
表示这是一个可变(mutating)函数
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?
这个不需要添加 mutating
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.
Swift 已经知道它是可变的了,因为它是可以被赋值的
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
Swift 知道它不是可变的。所以对于变量,Swift 是知道的
61
00:03:16,096 --> 00:03:19,264
if it's not a computed var, like look at this var,
如果它不是一个演算型变量(computed 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.
如果它是一个常量(let),它会被认为是不可改变的
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,
关注它是可变的(var)还是不可变的(let),对于演算属性
68
00:03:42,689 --> 00:03:47,192
whether it has both get and set.Everybody got all that? So
关注它是否可以获取和赋值。大家都听懂了吗
69
00:03:47,260 --> 00:03:49,060
it's only funcs that we have to put that on.
所以只需要将 mutating 放在函数的前面
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
那么,如果每次复制时,它的每一个位(bit)都被复制
80
00:04:17,291 --> 00:04:21,159
copy the bits of this entire thing every time. So instead,
程序就会变得非常低效。 所以 Swift 不那样做
81
00:04:21,228 --> 00:04:24,028
Swift is smart. It only actually copies the bits when
Swift 很聪明。它只复制你设置为可变的
82
00:04:24,097 --> 00:04:27,799
you mutate it. That's called copy-on-write semantics.
那一部分,这就叫做写时复制(copy on write)
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.
那么可能就会有 20 个指针指向同一个对象
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.
非常重要的主题,协议(protocol)
94
00:05:01,968 --> 00:05:05,737
It's the fourth pillar of building data structures
那是在 Swift 中建立数据结构的第四支柱
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
但是协议的使用,它在 Swift 语言与运行时
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.
东西调用一个应用程序接口(API)的方法
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 pass in
来决定它应该用你传递给它的参数做什么
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.
就像,还记得我们曾经讲过的 MVC 间的通信吗
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,
那些will, did, should, 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,
举个例子,在一个字典类型中,一个字典是哈希表(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.
所以字典的键(key)应该具有哈希性(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 hashbility, 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
字符串,数组,可数区间(countable range)它们都是
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,
协议就像其他这些类型一样,是头等类型(first-class type)
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,
方法和所有的变量,在 Swift 中是这样的,但是
197
00:10:22,255 --> 00:10:27,024
in Objective-C, that was not true. In Objective-C,
在 Objective-C 中,并不是这样的。 在 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
那么这是与 Swift 的很大的不同,而 Swift 处理它的方式