现在的工作中在使用MVVM(学习中),今天突然意识到之前对MVC理解错了。
现在的理解
应用就是与用户操作,处理一系列的数据后再呈现给用户。
model是处理这些数据用的,比如请求、映射、过滤、排序……
view是用来显示数据、与用户交互的。
controller是用来协调V与M的。
比如,举个例子,下面是部分代码。具体的请移步这里
Model.h
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, strong) NSArray *personArray;
- (void)fetchPersonArray;
@end
Model.m
#import "Model.h"
#import "Person.h"
@implementation Model
- (void)fetchPersonArray {
// 请求网络
// 或者
// 访问数据库
Person *person1 = [Person personWithName:@"aaaaa" age:1];
Person *person2 = [Person personWithName:@"bbbbb" age:2];
Person *person3 = [Person personWithName:@"ccccc" age:3];
Person *person4 = [Person personWithName:@"ddddd" age:4];
self.personArray = @[person1, person2, person3, person4];
}
@end
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;
@end
ViewController.m
#import "ViewController.h"
#import "Model.h"
#import "Person.h"
#import "View.h"
@interface ViewController ()
@property (nonatomic, strong) Model *model;
@property (nonatomic, strong) View *customView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.model = [[Model alloc] init];
[self.model addObserver:self forKeyPath:@"personArray" options:NSKeyValueObservingOptionNew context:nil];
self.customView = [[View alloc] init];
self.customView.frame = self.view.bounds;
[self.view addSubview:self.customView];
[self.model fetchPersonArray];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"personArray"]) {
[self updateUI];
}
}
- (void)updateUI {
self.customView.personName1.text = [self.model.personArray[0] name];
self.customView.personName2.text = [self.model.personArray[1] name];
self.customView.personName3.text = [self.model.personArray[2] name];
self.customView.personName4.text = [self.model.personArray[3] name];
}
@end
当然以上的理解还不敢说正确,但是可以肯定的是我之前是错了。
感觉挺惭愧的,错了这么久,谨记!!!
之前的理解(错误)
model是数据模型,比如上面中Person的角色
view是用来显示数据、与用户交互的。
controller是用来处理逻辑,协调V与M的。
将上面的例子用错误的方式实现如下,具体代码请移步这里
Model.h
#import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;
@end
ViewController.m
#import "ViewController.h"
#import "Model.h"
#import "View.h"
@interface ViewController ()
@property (nonatomic, strong) NSArray *personArray;
@property (nonatomic, strong) View *customView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.customView = [[View alloc] init];
self.customView.frame = self.view.bounds;
[self.view addSubview:self.customView];
__weak ViewController *weakSelf = self;
[self fetchPersonArray:^{
[weakSelf updateUI];
}];
}
- (void)fetchPersonArray:(void (^)(void))completion {
Model *person1 = [Model personWithName:@"aaaaa" age:1];
Model *person2 = [Model personWithName:@"bbbbb" age:2];
Model *person3 = [Model personWithName:@"ccccc" age:3];
Model *person4 = [Model personWithName:@"ddddd" age:4];
self.personArray = @[person1, person2, person3, person4];
if (completion) {
completion();
}
}
- (void)updateUI {
self.customView.personName1.text = [self.personArray[0] name];
self.customView.personName2.text = [self.personArray[1] name];
self.customView.personName3.text = [self.personArray[2] name];
self.customView.personName4.text = [self.personArray[3] name];
}
@end
网友评论
mvcs将model区分成了fat model 和thin model。区别是fat model 本身保研一定可复用的数据处理逻辑。来达到为c瘦身和代码复用的目的。
在mvcs上讲fat model 再次抽象其实可以理解成演变成现在的mvvm。也就是说 mvvm中的view model其实是mvcs 中fat model 的再次拆分结果再加上数据绑定