博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈iOS中MVVM的架构设计
阅读量:7221 次
发布时间:2019-06-29

本文共 3879 字,大约阅读时间需要 12 分钟。

  MVVM就是在MVC的基础上分离出业务处理的逻辑到viewModel层。

  M:  Model层是API请求的原始数据,充当DTO(数据传输对象),当然,用字典也是可以的,编程么,要灵活一些。Model层是比较薄的一层。

  V:  View层,视图展示,由viewController来控制,他的任务就是从ViewModel层获取数据,然后显示。

  VM:  ViewModel层负责业务处理和数据转化,就是View和Model层的粘合剂,他是一个放置用户输入验证逻辑,视图显示逻辑,发起网络请求和其他各种各样的代码的极好的地方。说白了,就是把原来ViewController层的业务逻辑和页面逻辑等剥离出来放到ViewModel层。

简单来说,就是API请求完数据,解析成model,之后在viewModel中转化成能够直接被视图层使用的数据,交付给前端。

model层

我们先从model层开始,在这里我们用JSONModel来解析,比如一个商品列表的model长这样:

这是我们的数据原型,API返回的数据通过JSONModel解析完成后的原始数据存在这里。

#import 
@protocol LVMProductListModel
@end// productList@interface LVMProductListModel : JSONModel@property (nonatomic, copy) NSString *imgUrl;@property (nonatomic, copy) NSString *productId;@property (nonatomic, copy) NSString *productName;@property (nonatomic, copy) NSString *refPrice;@end

viewModel 层

viewModel层是我们处理业务逻辑的核心层,在这里我们需要发起网络请求(如果网络请求较多,可以抽出来,只在ViewModel里调用)、解析数据、转换数据给前端。

#pragma mark - Public Methods- (void)lvm_startLoadProductListWithPage:(NSInteger)page {  __weak typeof(self) weakSelf = self;  [NetWorkManager GET:self.lvm_baseURL                   parameters:parameters                        success:^(NSURLSessionDataTask *task, id responseObject) {    __strong typeof(weakSelf) strongSelf = weakSelf;    ...    NSDictionary *resultDic = responseObject[@"rp_result"];    NSError *error = nil;    LVMProductListModel *model = [[LVMProductListModel alloc] initWithDictionary:resultDic error:&error];     if (error) {      ...    }    [strongSelf _lvm_calProductLists:model.productlist];    if (strangles.delegate ...) {       ...    }  } failure:^(NSURLSessionDataTask *task, NSError *error) {    ...  }];}- (void)_lvm_calProductLists:(NSArray *)productLists{  for (NSInteger i = 0; i < productLists.count; ++i) {    LVMProductListModel *model = productLists[i];    LVMProductListItem *item = [[LVMProductListItem alloc] init];    item.lvm_productId = model.productId;    item.lvm_productName = model.productName;    item.lvm_productPrice = [NSString stringWithFormat:@"¥ %@", model.refPrice];    item.lvm_productImgURL = [Utils convertToRealUrl:model.imgUrl ofsize:300];    [self.lvm_productLists addObject:item];  }}

viewModel中将API返回的数据解析成model,并将model转化成可供view层直接使用的item,将item交付给前端使用。

经过viewModel转化之后的数据itemviewModel保存,与数据相关的处理都将在viewModel中处理。viewModel返回给view层的接口长这样:

@interface LVMProductListViewModel (CollectionViewDataSource)- (NSInteger)lvm_numberOfItemsInSection:(NSInteger)section;- (LVMProductListItem *)lvm_itemForIndexPath:(NSIndexPath *)indexPath;@end

view层

view层是由viewController控制的。view层只做展示,不做业务处理。view层的数据由viewModel提供。view层看起来是这样的:

@implementation LVMProductListViewController- (void)viewDidLoad {  [super viewDidLoad];  self.view.backgroundColor = [UIColor whiteColor];  [self _lvm_initial];  [self _lvm_setupViewModel];  [self _lvm_setupSubViews];  [self.lvm_viewModel lvm_startLoadProductListWithPage:_lvm_currentPage];}- (void)_lvm_initial {  ...  self.lvm_currentPage = 1;}- (void)_lvm_setupViewModel {  self.lvm_viewModel = [[LVMProductListViewModel alloc] init];  _lvm_viewModel.lvm_delegate = self;}#pragma mark - Subviews -- (void)_lvm_setupSubViews {  ...  [self _lvm_setupCollectionView];  ...}- (void)_lvm_setupCollectionView {   ...}#pragma mark - UICollectionView Delegate & Datosource- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {  return [self.lvm_viewModel lvm_numberOfItemsInSection:section];}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {  LVMProductListItem *item = [self.lvm_viewModel lvm_itemForIndexPath:indexPath];  LVMProductListCollectionViewCell *cell = (LVMProductListCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kLVMProductListCollectionViewCellId forIndexPath:indexPath];  [cell lvm_setupWithItem:item];  return cell;}

 

转载地址:http://ifhym.baihongyu.com/

你可能感兴趣的文章
web前端性能优化指南
查看>>
解密翻译:爱的摩斯密码
查看>>
一天一道Shell练习题2
查看>>
图说 Java:理解 Java 机制最受欢迎的 8 幅图
查看>>
oracle内核参数检测和优化
查看>>
Varnish基础进阶
查看>>
NFS简介及自动挂载配置案例
查看>>
lvs和nginx的对比
查看>>
final修饰基本数据类型和修饰引用数据类型的区别
查看>>
Linux中常用的压缩和解压缩工具
查看>>
在Linux上设置web站点(三)——在httpd2.2中设置https服务
查看>>
HTTP入门知识
查看>>
斯坦福大学公开课:iPad和iPhone应用开发(iOS5) 学习笔记 1
查看>>
apt-get软件包管理工具
查看>>
集中式日志分析平台 - ELK Stack - 踩坑集锦
查看>>
Centos 7中部署LAMP
查看>>
使用css来显示xml数据
查看>>
[工具]Mac下非常好用的快捷终端Dterm
查看>>
PingingLab传世经典系列《CCNA完全配置宝典》-4.1 HDLC基本配置
查看>>
exchange2007 升级到 sp3 很ez
查看>>