iOS面试题收集(之MJExtension 字典转模型)

MJExtension是一套字典和模型之间互相转换的超轻量级框架

MJExtension能完成的功能

字典(JSON) --> 模型(Model)

模型(Model) --> 字典(JSON)

字典数组(JSON Array) --> 模型数组(Model Array)

模型数组(Model Array) --> 字典数组(JSON Array)

JSON array -> model array【字典数组转成模型数组】

NSArray *dictArray = @[
                         @{
                             @"name" : @"Jack",
                             @"icon" : @"lufy.png"
                         },
                         @{
                             @"name" : @"Rose",
                             @"icon" : @"nami.png"
                         }
                     ];

// JSON array -> User array
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
// Printing
for (User *user in userArray) {
    NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png

Model -> JSON【模型转成字典】

// New model
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";

Status *status = [[Status alloc] init];
status.user = user;
status.text = @"Nice mood!";

// Status -> JSON
NSDictionary *statusDict = status.mj_keyValues;
NSLog(@"%@", statusDict);
/*
 {
 text = "Nice mood!";
 user =     {
 icon = "lufy.png";
 name = Jack;
 };
 }
 */

// More complex situation
Student *stu = [[Student alloc] init];
stu.ID = @"123";
stu.oldName = @"rose";
stu.nowName = @"jack";
stu.desc = @"handsome";
stu.nameChangedTime = @"2018-09-08";

Bag *bag = [[Bag alloc] init];
bag.name = @"a red bag";
bag.price = 205;
stu.bag = bag;

NSDictionary *stuDict = stu.mj_keyValues;
NSLog(@"%@", stuDict);
/*
{
    ID = 123;
    bag =     {
        name = "\U5c0f\U4e66\U5305";
        price = 205;
    };
    desc = handsome;
    nameChangedTime = "2018-09-08";
    nowName = jack;
    oldName = rose;
}
 */

常见用法

  • key替换,比如ID和id
1.在模型类.m文件引入"MJExtension.h"
2.实现方法
+ (NSDictionary *)replacedKeyFromPropertyName {
    return @{@“非关键字的属性名” : @“数组的key”};
}
+ (NSDictionary *)replacedKeyFromPropertyName{ // 模型的desc属性对应着字典中的description
    return @{@"desc" : @"description",@"ID" : @"id"};
}
方法将model文件中定义的字段名转化为与请求数据中相同的,使两者的内部相同,只是名称不同

通过遇到有的字段是一个数组,数组中又存放了其他的数据模型对象,可以通过以下方法将其关联

+(NSDictionary *)objectClassInArray{
    return @{@"businesses" : [Busnisses class]};
}

实例一<模型中还有模型数组>

.h文件
#import 
#import "HWFindHeaderseItem.h"

@interface HWFindResponseItem : NSObject

@property(nonatomic, strong) HWFindHeaderseItem *header; // 直接是字典 直接定义要转的模型类型
@property(nonatomic, strong) NSArray *chics;    // 是模型数组 需要在.m文件中重写objectClassInArray方法
@property(nonatomic, strong) NSArray *featured;
@end
.m文件
#import "HWFindResponseItem.h"
#import 
#import "HWFindChicsItem.h"
#import "HWFindFeaturesItem.h"

@implementation HWFindResponseItem

+ (NSDictionary *)objectClassInArray{
    return @{@"chics" : [HWFindChicsItem class], @"featured" : [HWFindFeaturesItem class]};
}
@end

实例二<模型中属性与系统属性同名>

.h文件
#import 

@interface HWFindHeaderseItem : NSObject

@property(nonatomic, strong) NSString *title;
@property(nonatomic, strong) NSString *desc; // 与系统属性重名 需要在.m文件中重写replacedKeyFromPropertyName告知
@property(nonatomic, strong) NSArray *photos;
@end
.m文件
#import "HWFindHeaderseItem.h"
#import 

@implementation HWFindHeaderseItem
+ (NSDictionary *)replacedKeyFromPropertyName{ // 模型的desc属性对应着字典中的description
    return @{@"desc" : @"description"};
}
- (void)setTitle:(NSString *)title {
    _title = title;
    NSLog(@"%@", title);
}
@end

你可能感兴趣的:(iOS面试题收集(之MJExtension 字典转模型))