@@ -621,224 +621,207 @@ Text 和 HStack
621
621
622
622
151
623
623
00:07:05,290 --> 00:07:07,420
624
- At any time you should be able to say
624
+ 任何时刻你都可以让视图
625
625
626
626
152
627
627
00:07:07,420 --> 00:07:09,030
628
- make it look like the Model
628
+ 按照模型中的数据来显示
629
629
630
630
153
631
631
00:07:09,030 --> 00:07:11,900
632
- and so, we are gonna have a system
632
+ 在 Swift 中有一种机制
633
633
634
634
154
635
635
00:07:11,900 --> 00:07:13,530
636
- where whenever the Model changes,
636
+ 可以在模型发生变化时
637
637
638
638
155
639
639
00:07:13,530 --> 00:07:16,700
640
- it asks the View to look
641
- like it automatically.
640
+ 自动更新视图
642
641
643
642
156
644
643
00:07:16,700 --> 00:07:18,690
645
- Call that reactive programming,
644
+ 叫做响应式编程
646
645
647
646
157
648
647
00:07:18,690 --> 00:07:22,410
649
- it's reacting to changes in the Model.
648
+ 它会对模型中的变化作出响应
650
649
651
650
158
652
651
00:07:22,410 --> 00:07:23,470
653
- So that's it.
652
+ 就是这样一种机制
654
653
655
654
159
656
655
00:07:23,470 --> 00:07:26,303
657
- This is what we got to make happen
656
+ 我们引入视图模型(ViewModel)
658
657
659
658
160
660
659
00:07:27,980 --> 00:07:32,980
661
- is it introduces another
662
- thing called the ViewModel.
660
+ 来实现这种机制
663
661
664
662
161
665
663
00:07:33,044 --> 00:07:36,870
666
- The ViewModel's job is to
667
- bind the View to the Model.
664
+ 视图模型的工作就是将视图
665
+ 绑定到对应的模型上
668
666
669
667
162
670
668
00:07:36,870 --> 00:07:38,940
671
- So the one change has
672
- happened in the Model,
669
+ 模型中的数据一旦发生变化
673
670
674
671
163
675
672
00:07:38,940 --> 00:07:41,530
676
- the View get reflected.
673
+ 视图就会对其作出响应
677
674
678
675
164
679
676
00:07:41,530 --> 00:07:43,130
680
- And along the way,
677
+ 在视图模型
681
678
682
679
165
683
680
00:07:43,130 --> 00:07:45,920
684
- as it does that binding
685
- between the Model and the View,
681
+ 对模型和视图进行绑定的过程中
686
682
687
683
166
688
684
00:07:45,920 --> 00:07:49,150
689
- it might be interpreting
690
- the Model for the View
685
+ 会将模型中的数据转换为
686
+ 视图可以理解的形式
691
687
692
688
167
693
689
00:07:49,150 --> 00:07:50,980
694
- because we want the View to be very simple
690
+ 因为我们希望在声明式编程中
695
691
696
692
168
697
693
00:07:50,980 --> 00:07:53,870
698
- since we're writing it
699
- in a declarative way,
694
+ 视图能够尽量简洁
700
695
701
696
169
702
697
00:07:53,870 --> 00:07:55,790
703
- we don't want it to have
704
- a lot of code in there
698
+ 我们不希望视图中的代码过多
705
699
706
700
170
707
701
00:07:55,790 --> 00:07:58,750
708
- that's like converting from
709
- one data type to another
702
+ 例如数据类型转换之类的工作
710
703
711
704
171
712
705
00:07:58,750 --> 00:07:59,770
713
- and things like that.
706
+ 都不要交给视图
714
707
715
708
172
716
709
00:07:59,770 --> 00:08:02,320
717
- So we're gonna ask the
718
- ViewModel to do that.
710
+ 而是交给视图模型去做
719
711
720
712
173
721
713
00:08:02,320 --> 00:08:05,480
722
- Our Model in our game
723
- that we're writing here,
714
+ 我们这里构建的记忆游戏中的模型
724
715
725
716
174
726
717
00:08:05,480 --> 00:08:08,150
727
- this memory game, our Model
728
- is just gonna be a struct.
718
+ 是一个结构体
729
719
730
720
175
731
721
00:08:08,150 --> 00:08:10,130
732
- This is a very simple little demo
722
+ 这是一个非常简单的示例程序
733
723
734
724
176
735
725
00:08:10,130 --> 00:08:11,940
736
- but you could imagine that your Model
726
+ 但是你可以设想这里的模型
737
727
738
728
177
739
729
00:08:11,940 --> 00:08:15,560
740
- is a SQL database or it's
741
- something over the network
730
+ 是一个 SQL 数据库,或是向网络发出的
742
731
743
732
178
744
733
00:08:15,560 --> 00:08:17,610
745
- where you're making HTTP requests.
734
+ HTTP 请求
746
735
747
736
179
748
737
00:08:17,610 --> 00:08:19,760
749
- So it can be quite complicated over there
738
+ 这样的模型就相当复杂了
750
739
751
740
180
752
741
00:08:19,760 --> 00:08:22,460
753
- and your ViewModel can simplify that,
742
+ 视图模型可以将其封装到
754
743
755
744
181
756
745
00:08:22,460 --> 00:08:25,080
757
- boil it down into maybe
758
- some simpler data structures
746
+ 更简洁的数据结构中
759
747
760
748
182
761
749
00:08:25,080 --> 00:08:27,420
762
- that it can pass to the View
750
+ 之后再传递给视图
763
751
764
752
183
765
753
00:08:27,420 --> 00:08:30,340
766
- that will let the View be
767
- simple code that draws it.
754
+ 这样视图中就只有实现绘制的代码了
768
755
769
756
184
770
757
00:08:30,340 --> 00:08:32,760
771
- So, the ViewModel does
772
- have this role a little bit
758
+ 视图模型在这里扮演的角色
773
759
774
760
185
775
761
00:08:32,760 --> 00:08:36,053
776
- as an interpreter of that Model data.
762
+ 就像一个模型数据的解释者
777
763
778
764
186
779
765
00:08:36,990 --> 00:08:40,101
780
- So we're gonna interpose the ViewModel
766
+ 这样我们就引入了介于模型和视图
781
767
782
768
187
783
769
00:08:40,101 --> 00:08:41,960
784
- between the Model and the View.
770
+ 之间的视图模型
785
771
786
772
188
787
773
00:08:41,960 --> 00:08:44,750
788
- It's gonna be the thing that
774
+ 它帮助视图实现
789
775
790
776
189
791
777
00:08:44,750 --> 00:08:48,020
792
- helps make this automatic
793
- updating happening.
778
+ 自动更新
794
779
795
780
190
796
781
00:08:48,020 --> 00:08:50,430
797
- So how does it do what it does?
782
+ 那么它是如何做到的呢?
798
783
799
784
191
800
785
00:08:50,430 --> 00:08:51,890
801
- Well, first of all the ViewModel
786
+ 首先,视图模型
802
787
803
788
192
804
789
00:08:51,890 --> 00:08:54,710
805
- is always trying to notice
806
- changes in the Model
790
+ 会随时关注模型的变化
807
791
808
792
193
809
793
00:08:54,710 --> 00:08:57,390
810
- and it can do that any way that it wants.
794
+ 它可以通过任何方式实现
811
795
812
796
194
813
797
00:08:57,390 --> 00:09:00,900
814
- If your Model is a struct,
815
- it's actually quite easy.
798
+ 如果你的模型是一个结构体
799
+ 那相当容易
816
800
817
801
195
818
802
00:09:00,900 --> 00:09:02,600
819
- Again, I'm gonna talk about a struct,
803
+ 我又一次谈到了结构体
820
804
821
805
196
822
806
00:09:02,600 --> 00:09:05,450
823
- more about that type in
824
- Swift in a few moments
807
+ 之后还会讲到更多关于 Swift 中
808
+ 结构体这个类型的特性
825
809
826
810
197
827
811
00:09:05,450 --> 00:09:07,980
828
- but one great feature of a struct
812
+ 其中一个重要的特性是
829
813
830
814
198
831
815
00:09:07,980 --> 00:09:09,460
832
- since it's copied around
816
+ 因为将结构体
833
817
834
818
199
835
819
00:09:09,460 --> 00:09:11,431
836
- when it's passed to functions and stuff
820
+ 传递给函数时会被复制一份
837
821
838
822
200
839
823
00:09:11,431 --> 00:09:15,030
840
- is that Swift knows when
841
- a struct has changed.
824
+ 所以 Swift 知道结构体发生变化的时机
842
825
843
826
201
844
827
00:09:15,030 --> 00:09:16,998
0 commit comments