美文网首页
IOS框架:使用网络请求类框架

IOS框架:使用网络请求类框架

作者: 时光啊混蛋_97boy | 来源:发表于2020-11-05 09:49 被阅读0次

原创:知识点总结性文章
创作不易,请珍惜,之后会持续更新,不断完善
个人比较喜欢做笔记和写总结,毕竟好记性不如烂笔头哈哈,这些文章记录了我的IOS成长历程,希望能与大家一起进步
温馨提示:由于简书不支持目录跳转,大家可通过command + F 输入目录标题后迅速寻找到你所需要的内容

目录

  • 一、YYModel
  • Demo
  • 参考文献

一、YYModel

在iOS开发中总会用到各种JSON与模型相互转换的需求,之前的项目中一直使用MJExtension,但是最近发现一个轻量级的库YYModel,使用简单,性能也很不错,接下来就说说YYModel的一些简单的用法及注意事项。

a、JSON转换为模型,或者模型转换为JSON
// JSON
{
    accountId = "<null>";
    avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
    unionId = o2t87wKAR1pNRZR0cnReBT608mok;
    userid = 11703346;
    wxNickName = "\U90a3\U5496";
}

// Model
@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;

// JSON转换为模型
FansModel *fansModel = [FansModel yy_modelWithJSON:dic];
FansModel *fansModel = [FansModel yy_modelWithDictionary:dic];

// 模型转换为JSON
NSDictionary *json = [fansModel yy_modelToJSONObject];
b、手动创建的模型和服务端返回的模型属性名称不一致时
// JSON
{ "merchantId" = "<null>";
  "avatar" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
  "unionID" = "o2t87wKAR1pNRZR0cnReBT608mok";
  "userID" = "11703346";
  "userName" = "贺建奎";
}

// Model
@interface StarModel : NSObject <YYModel>

@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;

@end

// 手动创建的模型和服务端返回的模型属性名称不一致时
+ (NSDictionary<NSString *, id> *)modelCustomPropertyMapper
{
    return @{@"avatarUrl" : @"avatar",
             @"wxNickName" : @"userName",
             @"accountId" : @"merchantId",
             @"unionId" : @"unionID",
             @"userid":@"userID",
             };
}

// JSON转换为模型
NSDictionary *dict;
StarModel *fansModelWithJSON = [StarModel yy_modelWithJSON:dict];
StarModel *fansModelWithDictionary = [StarModel yy_modelWithDictionary:dict];
c、模型套模型
JSON
{
    citys =   (
                {
            area = "<null>";
            areaId = "<null>";
            areas =             (
                                {
                    area = "\U4e1c\U57ce\U533a";
                    cityid = 110100;
                    id = 110101;
                    status = 1;
                },
                                {
                    area = "\U5e73\U8c37\U533a";
                    cityid = 110100;
                    id = 110117;
                    status = 1;
                }
            );
            city = "\U5e02\U8f96\U533a";
            cityId = "<null>";
            id = 110100;
            province = "<null>";
            provinceid = 110000;
            status = 1;
        },
                {
            area = "<null>";
            areaId = "<null>";
            areas =             (
                                {
                    area = "\U5bc6\U4e91\U53bf";
                    cityid = 110200;
                    id = 110228;
                    status = 1;
                },
                                {
                    area = "\U5ef6\U5e86\U53bf";
                    cityid = 110200;
                    id = 110229;
                    status = 1;
                }
            );
            city = "\U53bf";
            cityId = "<null>";
            id = 110200;
            province = "<null>";
            provinceid = 110000;
            status = 1;
        }
    );
    id = 110000;
    province = "\U5317\U4eac\U5e02";
    status = 1;
}
.h文件
// BaseModel 只是为了将 id 映射为 _id
@interface BaseModel : NSObject <YYModel>

// id 映射为 _id
@property (nonatomic, copy) NSString *_id;

@end

// 地区
@interface AreaModel : BaseModel

@property (nonatomic, copy) NSString *area;
@property (nonatomic, copy) NSString *cityid;
@property (nonatomic, copy) NSString *status;

@end

// 城市
@interface CityModel : BaseModel

@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *provinceid;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, strong) NSArray *areas;

@end

// 省份
@interface ProvinceModel : BaseModel

@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, strong) NSArray *citys;

@end

@interface MapViewModel : NSObject

@end
.m文件
@implementation BaseModel

// 将 id 映射为 _id
+(NSDictionary<NSString *,id> *)modelCustomPropertyMapper
{
    // 由于对accountId做了校验,所以如果accountId不符合要求,该JSON将不会被解析
    return @{@"_id": @"id"};
}

@end

@implementation AreaModel

@end

@implementation CityModel

// 模型套模型
+ (NSDictionary *)modelContainerPropertyGenericClass
{
    return @{@"areas" : [AreaModel class]};
}

@end

@implementation ProvinceModel

// 模型套模型
+ (NSDictionary *)modelContainerPropertyGenericClass
{
    return @{@"citys" : [CityModel class]};
}

@end

@implementation MapViewModel

- (void)modelContainer
{
    //将JSON转换为模型
    NSDictionary *dict;
    ProvinceModel *provinceModel = [ProvinceModel yy_modelWithJSON:dict];
    CityModel *cityModel = provinceModel.citys[0];
    AreaModel *areaModel = cityModel.areas[0];
}

@end
d、数组里面存放字典类型转换为模型
JSON
(
{
"accountId" = "<null>";
"avatarUrl" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
"unionId" = "o2t87wKAR1pNRZR0cnReBT608mok";
"userid" = "11703346",
"wxNickName" = "谢佳培"
},
{
"accountId" = "<null>";
"avatarUrl" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
"unionId" = "o2t87wKAR1pNRZR0cnReBT608mok";
"userid" = "11293020";
"wxNickName" = "兔子";
}
);
.h文件
@interface RabbitModel : NSObject

@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;

@end

@interface RabbitViewModel : NSObject

@end
.m文件
@implementation RabbitModel

@end

@implementation RabbitViewModel

// 转换为模型
- (void)manyRabbit
{
    NSArray *array;
    NSArray<RabbitModel *> *rabbitModelArray = [NSArray yy_modelArrayWithClass:[RabbitModel class] json:array];
}

@end
e、容器类属性
JSON
{
"schoolName" = "厦门大学";
"teachers" =  (
      {"teacherName"= "Sndday","teacherAge" = 50};
      {"teacherName" = "Ssasas","teacherAge" = 70};
      {"teacherName" = "Snddasa","teacherAge"= 30)}
      );
};
.h文件
// 学校
@interface SchoolModel : NSObject <YYModel>

@property (nonatomic, copy) NSString *schoolName;
@property (nonatomic, copy) NSArray *teachers;

@end

// 老师
@interface TeacherModel : NSObject

@property (nonatomic, copy) NSString *teacherName;
@property (nonatomic, assign) NSInteger teacherAge;

@end

@interface SchoolViewModel : NSObject

@end
.m文件
@implementation SchoolModel

+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass
{
    return @{@"teachers":[TeacherModel class]};
}

@end

@implementation TeacherModel

@end

@implementation SchoolViewModel

- (void)teachStudent
{
    // 转换为模型
    NSDictionary *dict = @{
      @"schoolName":@"厦门大学",
      @"teachers":@[
              @{@"teacherName":@"Sndday",@"teacherAge":@(50)},
              @{@"teacherName":@"Ssasas",@"teacherAge":@(70)},
              @{@"teacherName":@"Snddasa",@"teacherAge":@(30)}
              ],
      };

    SchoolModel *model = [SchoolModel yy_modelWithJSON:dict];
    TeacherModel *teacherModel = model.teachers[0];
}

@end
f、黑名单,即加入黑名单的字段会被忽略不会转换
JSON
{
    accountId = "<null>";
    avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
    unionId = o2t87wKAR1pNRZR0cnReBT608mok;
    userid = 11703346;
    wxNickName = "\U90a3\U5496";
}
.h文件
@interface BlacklistModel : NSObject <YYModel>

@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;

@end

@interface BlacklistViewModel : NSObject

@end
.m文件
@implementation BlacklistModel

// 加入黑名单的字段会被忽略不会转换
+ (NSArray *)modelPropertyBlacklist
{
    return @[@"avatarUrl", @"accountId"];
}

@end

@implementation BlacklistViewModel

- (void)blackList
{
    // 将JSON转换为模型,转换出来的模型中的avatarUrl和accountId的值为nil
    NSDictionary *dict;
    BlacklistModel *blacklistModel = [BlacklistModel yy_modelWithJSON:dict];
}

@end
g、白名单,即只处理加入白名单的字段,其余字段忽略掉
JSON
{
    accountId = "<null>";
    avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
    unionId = o2t87wKAR1pNRZR0cnReBT608mok;
    userid = 11703346;
    wxNickName = "\U90a3\U5496";
}
.h
@interface WhitelistModel : NSObject <YYModel>

@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;

@end

@interface WhitelistViewModel : NSObject

@end
.m
@implementation WhitelistModel

// 只处理加入白名单的字段,其余字段忽略掉
+ (NSArray *)modelPropertyWhitelist
{
    return @[@"avatarUrl", @"accountId"];
}

@end

@implementation WhitelistViewModel

- (void)whiteList
{
    // 将JSON转换为模型
    NSDictionary *dict;
    WhitelistModel *whitelistModel = [WhitelistModel yy_modelWithJSON:dict];
}

@end
h、数据校验与自定义转化
JSON
{
    accountId = "<null>";
    avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
    unionId = o2t87wKAR1pNRZR0cnReBT608mok;
    userid = 11703346;
    wxNickName = "\U90a3\U5496";
}
.h文件
@interface CustomTransformModel : NSObject <YYModel>

@property (nonatomic, strong) NSString *avatarUrl;
@property (nonatomic, strong) NSString *wxNickName;
@property (nonatomic, strong) NSString *accountId;
@property (nonatomic, strong) NSString *unionId;
@property (nonatomic, strong) NSString *userid;
@property (nonatomic, strong) NSString *resultStr;

@end

@interface CustomTransformViewModel : NSObject

@end
.m文件
@implementation CustomTransformModel

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dict
{
    NSString *timestamp = dict[@"accountId"];
    if ([timestamp isEqualToString:@"<null>"])
    {
         return NO;
    }
    
    _resultStr = [NSString stringWithFormat:@"将后端返回的时间戳转换为时间字符串:%@",timestamp];
    return YES;
}

@end

@implementation CustomTransformViewModel

- (void)customTransform
{
    // 将JSON转换为模型
    NSDictionary *dict;
    CustomTransformModel *customTransformModel = [CustomTransformModel yy_modelWithJSON:dict];
}

@end

Demo

Demo在我的Github上,欢迎下载。
UseFrameworkDemo

参考文献

相关文章

网友评论

      本文标题:IOS框架:使用网络请求类框架

      本文链接:https://www.haomeiwen.com/subject/dxdovktx.html