MJExtension 模型转换
起因
来了新公司,看看上一手的代码,如果上一手,有使用过类似的工具就不会导致现在呢么多坑.
快速引用
#import "MJExtension.h"
MJCodingImplementation
+ (NSDictionary *)replacedKeyFromPropertyName{
return @{@"hashString":@"hash"};
}
+ (NSDictionary *)mj_objectClassInArray{
return @{
@"newsList":[TTVHomeChannelNewsModelNewsList class],
@"headlineList":[TTVHomeChannelNewsModelNewsList class],
};
}
MJExtension 归档
测试程序
NSString *UserId = [NSString stringWithFormat:@"%ldAAAAAAATTTTTTTT.data", [PKUserInfoTool userInfo].uid];
NSString *PKPostLablePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:UserId];
NSMutableArray *ary = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
[ary addObject:[NSArray arrayWithObjects:[NSString stringWithFormat:@"label%d",i],@"123",@"456", nil]];
}
PKLog(@"pring in ram%@", ary);
PKLabelSelectionModel *data = [[PKLabelSelectionModel alloc] init];
data.ids = ary;
//写入数组
[[NSFileManager defaultManager] removeItemAtPath:PKPostLablePath error:nil];
[NSKeyedArchiver archiveRootObject:data toFile:PKPostLablePath];
//读取数组
PKLabelSelectionModel *read = [NSKeyedUnarchiver unarchiveObjectWithFile:PKPostLablePath];
PKLog(@"read from local:%@", read.ids);
PKLabelSelectionModel.c
#import "PKLabelSelectionModel.h"
#import "MJExtension.h"
@implementation PKLabelSelectionModel
MJCodingImplementation
@end
PKLabelSelectionModel.h
#import <Foundation/Foundation.h>
@interface PKLabelSelectionModel : NSObject
//名字
@property (nonatomic, strong) NSMutableArray* names;
//id
@property (nonatomic, strong) NSMutableArray* ids;
@end
MJExtension 解析JSON
import “MJExtension.h”
建立模型
程序模型
#import <Foundation/Foundation.h>
@interface PKQuestionDate : NSObject
// isOpen int 是否开启问答(0代表不开启,1代表开启)
@property (nonatomic, assign) int isOpen;
//开启每轮问题的时间,例:'20:25'(isOpen为0时不填此参数)
@property (nonatomic, assign) NSString *startTime;
//问答开启的星期数,例:0,0,1,0,1,0,1,表示星期三、五、日,触发新一轮(isOpen为0时不填此参数)
@property (nonatomic) NSMutableArray *qaDay;
//一轮可回答的问题条数(isOpen为0时不填此参数)
@property (nonatomic, assign)int questionCount;
//是否开启有新提问时通知我(isOpen为0时不填此参数)
@property (nonatomic, assign)int isNoticeMe;
@end
字典模型
参数名 | 类型 | 说明 |
---|---|---|
uid | long | 用户ID |
sid | string | 会话 id |
isOpen | int | 是否开启问答(0代表不开启,1代表开启) |
startTime | string | 开启每轮问题的时间,例:‘20:25’(isOpen为0时不填此参数) |
qaDay | int[] | 问答开启的星期数,例:0,0,1,0,1,0,1,表示星期三、五、日,触发新一轮(isOpen为0时不填此参数) |
questionCount | int | 一轮可回答的问题条数(isOpen为0时不填此参数) |
isNoticeMe | int | 是否开启有新提问时通知我(isOpen为0时不填此参数) |
转换数据使用
PKQuestionDate *data = [PKQuestionDate mj_objectWithKeyValues:dict[@"data"]];
self.openFlag = data.isOpen;