File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -95,9 +95,49 @@ self.title = [self.viewModel.initialPhotoModel photoName];
9595我们来完成实现文件里的东西。第一件事就是:我们需要#import `FRPPhotoModel`类的头文件。然后,我们将打开私有属性的读写访问权限。
9696
9797```Objective-C
98+ //Model
99+ #import "FRPPhotoModel.h"
98100
101+ @interface FRPFullSizePhotoViewModel ()
102+ //private access
103+ @property (nonatomic, assign) NSInteger initialPhotoIndex;
99104
105+ @end
100106
101107```
108+ 好!下一步处理我们的初始化方法
109+
110+ ``` Objective-C
111+ - (instancetype )initWithPhotoArray:(NSArray *)photoArray initialPhotoIndex:(NSInteger )initialPhotoIndex {
112+ self = [super initWithModel:photoArray];
113+ if(!self) return nil;
114+
115+ self.initialPhotoIndex = initialPhotoIndex;
116+
117+ return self;
118+ }
119+ ```
120+
121+ 初始化方法中,先调用超类的` initWithModel: ` 实现,然后设置自己的` initialPhotoIndex ` 属性。剩下的两个只读属性的获取逻辑微不足道。
122+
123+ ``` Objective-C
124+ - (NSString *)initialPhotoName {
125+ return [[self photoModelAtIndex:self.initialPhotoIndex] photoName];
126+ }
127+
128+ - (FRPPhotoModel *)photoModelAtIndex:(NSInteger )index {
129+ if(index < 0 || index > self.model.count - 1) {
130+ //Index was out of bounds, return nil
131+ return nil;
132+ }
133+ else {
134+ return self.model[ index ];
135+ }
136+ }
137+
138+ ```
139+
140+ 这样做的另一个优点是:业务逻辑不需要重复书写,而且也使得这个非常好进行单元测试。
141+
102142
103143
You can’t perform that action at this time.
0 commit comments