iOS开发

iOS架构初探

2018-12-28  本文已影响50人  BlackStar暗星

Demo传送门
此架构是本人根据自己的工作经验及网络了解外加同事的意见整理出来的

一千个观众眼中有一千个哈姆雷特(There are a thousand Hamlets in a thousand people's eyes.)-- 莎士比亚

一千个程序员中眼里有一千种架构 (There are a thousand framework in a thousand developer's eyes.)-- BlackStar

和莎士比亚并列有木有很高端, 哈哈哈 有点装逼了 sorry



言归正传,由于老大要求重构项目,让我们研究下架构,那好吧,虽然啥都不懂,但是对这方面还是希望有所了解的,所以 baidu、google、github ...

经过一番 查找 研究,发现嗯...

架构这东西就是为了更好的贴合业务,便于测试,查找问题,修改,后续他人接手

反正好处一堆一堆的,但是我是小白啊,一次怎么可能弄那么高端,那就别好高骛远,先整个初级的,然后经过一周的研究,整理出来这个模式。我称之为 BSMVVM,BS就是个人昵称BlackStar,之所以这么叫就是因为这个架构只是我自己的MVVM,并不是网络流传的MVVM。

UI上全部采用 Masonry 布局,个别需要用到frame的灵活运用
Model 全部采用 YYModel 进行解析

目录结构:

目前由于老大的要求,我们建立了很多 Base基类 包含ViewController、Model、ViewModel、DataManager,其实View 也是有的,但是感觉没有用到,就没有用,先说下Base里做的事情

if ([self.delegate respondsToSelector:@selector()]) {
    [self.delegate ...];
}
#import "SQBaseViewModel.h"

@class SQPropertyPaymentDetailCell;
@class SQProPayHistoryHeaderView;

@interface SQProPayViewModel : SQBaseViewModel

-(void)propertyPaymentDetailHeaderView:(SQProPayHistoryHeaderView*)headerView houseInfo:(NSDictionary *)houseInfo searchType:(NSString *)searchType;

-(void)propertyPaymentDetailCell:(SQPropertyPaymentDetailCell *)cell  cellInfoKey:(NSString*)key cellInfoValue:(NSString*)value;

@end

.m文件

#import "SQProPayViewModel.h"
#import "SQPropertyPaymentDetailCell.h"
#import "SQProPayHistoryHeaderView.h"

@implementation SQProPayViewModel

-(void)propertyPaymentDetailCell:(SQPropertyPaymentDetailCell *)cell cellInfoKey:(NSString *)key cellInfoValue:(NSString *)value{
    
    if ([key isEqualToString:@"watermarkImg"]) {
        if ([value isEqualToString:@"1"]) {
            cell.watermarkImg.image = [UIImage imageNamed:@"pay_finish_recieve"];
        }else{
            cell.watermarkImg.image = [UIImage imageNamed:@"pay_finish_refund"];
        }
    }else{
        
        if ([key isEqualToString:@"houseLabel"]) {
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"房间:%@",value?:@""]];
            [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"666666"] range:NSMakeRange(0, 3)];
            cell.houseLabel.attributedText = attr;
            
        }else if ([key isEqualToString:@"houseKeeper"]){
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"业主:%@",value?:@""]];
            [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"666666"] range:NSMakeRange(0, 3)];
            cell.houseKeeper.attributedText = attr;
            
        }else if ([key isEqualToString:@"telLabel"]){
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"手机:%@",value?:@""]];
            [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"666666"] range:NSMakeRange(0, 3)];
            cell.telLabel.attributedText = attr;
            
        }else if ([key isEqualToString:@"timeLabel"]){
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"收费时间:%@",value?:@""]];
            [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"666666"] range:NSMakeRange(0, 5)];
            cell.timeLabel.attributedText = attr;
            
        }else{
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"收费人:%@",value?:@""]];
            [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"666666"] range:NSMakeRange(0, 4)];
            cell.tollCollector.attributedText = attr;
        }
    }
}


-(void)propertyPaymentDetailHeaderView:(SQProPayHistoryHeaderView *)headerView houseInfo:(NSDictionary *)houseInfo searchType:(NSString *)searchType{

    if (!houseInfo) return;
    
    if ([searchType isEqualToString:@"1"]) {

        NSString *addresStr = [NSString stringWithFormat:
                               @"%@%@%@%@%@%@%@%@",
                               houseInfo[@"manageAreaName"],
                               houseInfo[@"projectName"],
                               houseInfo[@"buildingNumber"],
                               houseInfo[@"buildingName"],
                               houseInfo[@"unitNumber"],
                               houseInfo[@"unitName"],
                               houseInfo[@"houseNumber"],
                               houseInfo[@"houseName"]];
        
        headerView.addressLabel.text = addresStr;
    }else{
        
        headerView.nameLabel.text = [NSString stringWithFormat:@"业主:%@   %@",houseInfo[@"name"],houseInfo[@"phone"]];
    }
}

@end

其中需要注意的点:

  1. .h文件中使用@class引入,告诉编译器这个是个类,不要报错,然后在.m中 #import , 这样做的目的就是减少编译时间
  2. 要使用此方式,需要所有的 view 将属性(控件)全部声明在.h 中,以便调用和 复用的时候UI 微调。之所以这么设计是参考了storyboard的设计理念。xcode创建出来的view 在.m中是没有@interface的,意思就是建议我们将 视图属性添加在.h中,所以我们在用storyboard做view的构建时,都是拉线到.h中,只有viewcontroller的才会在.m中。而且这么设计最大的好处是复用,不是所有UI都是一样的设计,但可能很相近,我们做一个控件应该尽可能的让其胜任多种情况。
#import "SQBaseDataManager.h"

@class SQPropertyPaymentListModel;
@class SQProPaymentPreviewModel;
@class SQProPayHistoryModel;
@class SQProPayBillPreviewDetailModel;
@class SQProPreviewPayInfoModel;

@interface SQPropertyPaymentDataManager : SQBaseDataManager

@property (nonatomic,strong) SQPropertyPaymentListModel *proPaymentListModel;       //物业缴费列表
@property (nonatomic,strong) SQProPaymentPreviewModel *proPaymentPreviewModel;      //物业缴费预览模型
@property (nonatomic,strong) SQProPayHistoryModel *historyModel;                    //物业缴费历史记录模型
@property (nonatomic,strong) SQProPayBillPreviewDetailModel *tollModel;             //缴费单详情模型
@property (nonatomic,strong) SQProPreviewPayInfoModel *payInfoModel;                //缴费信息模型

/**
 获取物业缴费单

 @param addressId 房屋对硬的地区ID
 @param categoryId  1:常规费用 | 2:临时费用
 */
-(void)getPropertyPaymentFeeListWithAddressId:(NSString *)addressId categoryId:(NSString *)categoryId block:(void(^)(SQPropertyPaymentListModel *classifyModel,NSString* errMsg))block;

.m文件

-(void)getPropertyPaymentFeeListWithAddressId:(NSString *)addressId categoryId:(NSString *)categoryId block:(void (^)(SQPropertyPaymentListModel *, NSString *))block{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/apps/bpps/bpp/bill/roomFeeSum",LocalCfg.SQ_PROPERTY_BASE];
    
    NSDictionary *param = @{@"addressId":addressId,
                            @"categoryId":categoryId};
    
    SQLeApiRequest *request = [SQLeApiRequest requestWithUrl:url param:param];
    request.requestMethod = REQUEST_GET;
    [request setLeHeader:@{@"Authorization":@"yes"} needSign:NO needAccessToken:YES];
    
    [request startRequestWithSuccess:^(SQApiResponse *response) {
       
        SQPropertyPaymentListModel *classifyModel = [SQPropertyPaymentListModel modelWithOriginData:response.responseObject];
        self.proPaymentListModel = classifyModel;
        block(classifyModel,nil);
        
    } failure:^(NSError *error) {
        block(nil,@"请求失败,请重试");
    }];    
}

  1. 设计考虑到数据模型使用问题,因为dataManager 是要在VC层声明的,所以将model声明在了dataManager里,网络回调后在dataManager里直接赋值,包含加载更多后的数据拼接等,都可以在dataManager里去实现,帮controller分担了数据整合的问题
  2. 缓存的调用也是在dataManager里实现的,在调用方法的同时block先回调缓存的数据,数据回来后,在此block回调接口数据

目前总结的差不多就是这此研究架构的一些心得,在开发中我们需要便捷开发,快速开发,很多重复性的工作我们应该尽可能的只做一遍,所以像view层的 protocol 我这里都是创建的时候自带的,不用一遍一遍去声明。

Demo传送门
如何创建文件自动生成我们常用的代码,点击进入

对于架构的初探还有一些没有写出来,比如说网络层、工具层,后续可能会补也可能不补

上一篇 下一篇

猜你喜欢

热点阅读