iOS 模型转换-MJExtension

  • MJExtension是一套字典和模型之间互相转换的超轻量级框架
  • JSON --> ModelCore Data Model
  • JSONString --> ModelCore Data Model
  • ModelCore Data Model --> JSON
  • JSON Array --> Model ArrayCore Data Model Array
  • JSONString --> Model ArrayCore Data Model Array
  • Model ArrayCore Data Model Array --> JSON Array
  • Coding all properties of a model with only one line of code.
    • 只需要一行代码,就能实现模型的所有属性进行Coding(归档和解档)
pod 'MJExtension'

 MJExtension的详细说明见GitHub:https://github.com/CoderMJLee/MJExtension

引入三方库头文件,如下:

#import "MJExtension.h"

LSBaseModel.h  基类头文件代码

//
//  LSBaseModel.h
//  iPhone
//
//  Created by xujinzhong on 16/3/11.
//  Copyright (c) 2016年 ZZLL. All rights reserved.
//

@interface LSBaseModel : NSObject

@end

LSBaseModel.m  

//
//  LSBaseModel.m
//  iPhone
//
//  Created by xujinzhong on 16/3/11.
//  Copyright (c) 2016年 ZZLL. All rights reserved.
//

#import "LSBaseModel.h"

@implementation ZLBBaseModel

/**
 *  将属性名换为其他key去字典中取值
 *
 *  @return 字典中的key是属性名,value是从字典中取值用的key
 */
+(NSDictionary *)replacedKeyFromPropertyName
{
    return @{};
}

/**
 *  数组中需要转换的模型类
 *
 *  @return 字典中的key是数组属性名,value是数组中存放模型的Class(Class类型或者NSString类型)
 */
+(NSDictionary *)objectClassInArray
{
    return @{};
}

@end

 

wordModel.h

//
//  wordModel.h
//  wordApplication
//
//  Created by zlw on 2017/12/18.
//  Copyright © 2017年 jinzhong xu. All rights reserved.
//

#import "ZLBBaseModel.h"

@interface wordModel : ZLBBaseModel

@property(nonatomic, strong) NSString   *ID;        //单词索引
@property(nonatomic, strong) NSString   *Word;      //单词名称
@property(nonatomic, strong) NSString   *yinbiao;   //单词音标
@property(nonatomic, strong) NSString   *Cnmean;    //单词汉语解释
@property(nonatomic, strong) NSString   *Enmean;    //单词英文解释
@property(nonatomic, strong) NSString   *FS;        //单词复数
@property(nonatomic, strong) NSString   *GQS;       //单词过去式
@property(nonatomic, strong) NSString   *GQFC;      //单词过去分词
@property(nonatomic, strong) NSString   *XZFC;      //单词现在分词
@property(nonatomic, strong) NSString   *Split;     //单词词缀分解
@property(nonatomic, strong) NSString   *Affix;     //单词词缀
@property(nonatomic, strong) NSString   *Lj;        //单词例句
@property(nonatomic, assign) NSInteger  YD;         //已读
@property(nonatomic, assign) NSInteger  SC;         //生词
@property(nonatomic, assign) NSInteger  BX;         //不学
@property(nonatomic, strong) NSString   *DT;        //日期


@end

wordModel.m

//
//  wordModel.m
//  wordApplication
//
//  Created by zlw on 2017/12/18.
//  Copyright © 2017年 jinzhong xu. All rights reserved.
//

#import "wordModel.h"

@implementation wordModel

+(NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{
             @"ID":@"ID",           //单词索引
             @"Word":@"Word",       //单词名称
             @"yinbiao":@"yinbiao", //单词音标
             @"Cnmean":@"Cnmean",   //单词汉语解释
             @"Enmean":@"Enmean",   //单词英文解释
             @"FS":@"FS",           //单词复数
             @"GQS":@"GQS",         //单词过去式
             @"GQFC":@"GQFC",       //单词过去分词
             @"XZFC":@"XZFC",       //单词现在分词
             @"Split":@"Split",     //单词词缀分解
             @"Affix":@"Affix",     //单词词缀
             @"Lj":@"Lj",           //单词例句
             @"YD":@"YD",           //已读
             @"SC":@"SC",           //生词
             @"BX":@"BX",           //不学
             @"DT":@"DT",           //日期
             };
}

@end

 

posted on 2018-02-27 16:47  东方🐺  阅读(231)  评论(0编辑  收藏  举报

导航