美文网首页
Mantle库替换为MJExtension

Mantle库替换为MJExtension

作者: 不动科气坏了 | 来源:发表于2019-02-21 21:12 被阅读0次

        项目历史原因,混合使用了MJExtension与Mantle框架。但是在遇到保留字段时遇到了问题,猜测原因可能库冲突,所以将Mantle库替换为MJExtension。总体还是很简单的,重点记录一下MJExtension的使用。

        Mantle与MJExtension皆是一种字典模型转换框架,简单使用过后感觉MJExtension更好用。

        Mantle: 需要继承MTLModel 遵循协议MTLJSONSerializing

        实现+ (NSDictionary *)JSONKeyPathsByPropertyKey ;

        在属性中有日期类型是需要转换

        + (NSDateFormatter*)dateFormatter {

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

        dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZ";

        return dateFormatter;

        }

        + (NSValueTransformer *)dateJSONTransformer {

            return [MTLValueTransformer transformerUsingForwardBlock:^(NSString *string, BOOL *success,     NSError *__autoreleasing *error) {

            return [[super dateFormatter] dateFromString:string];

        } reverseBlock:^(NSDate *date, BOOL *success, NSError *__autoreleasing *error) {

            return [[super dateFormatter] stringFromDate:date];

        }];

    }

    常用方法:

        NSArray *events = [MTLJSONAdapter modelsOfClass:[SHQEvent class]             fromJSONArray:responseObject error:nil];

        SHQEvent *event = [MTLJSONAdapter modelOfClass:[SHQEvent class] fromJSONDictionary:[responseObject objectForKey:@"result"] error:nil];


        MJExtension:可以看到基本是写在分类中的,不需要继承,直接导入实现相应方法即可。

            github地址:https://github.com/CoderMJLee/MJExtension

    MJExtension

    无论是字典转模型还是jsonString转模型,包括模型中嵌套了模型,只要想转换为模型,调用方法

    Model *model = [Model mj_objectWithKeyValues:param];

    模型中有数组 ,数组中又有其他模型

    [Model mj_setupObjectClassInArray:^NSDictory * {

        return @{ @“events”:@“Event”} // 模型数组转换

    [Model mj_objectWithKeyValues:dic];

    模型中属姓名和字典中的key不一样(最常用)

    Model mj_setupReplacedKeyFromPropertyName:^NSDictory  {

    return @{@“userName”:@“username”};

    }

    Model *model = [Model mj_objectWithKeyValues:dic];

    模型转换为字典:

    NSDictionary *dic = Model.mj_keyValues;

    字典数组转换为模型数组

    NSArray *models = [Model mj_objectArrayWithKeyValuesArray:dic];

    模型数组转换为字典数组:

    NSArray *dicArray = [Model mj_keyValuesArrayWithObjectArray:arr];

    不转换某个属性

    // what properties not to be coded

    [Model mj_setupIgnoredCodingPropertyNames:^NSArray *{

        return @[@""];

    }];

    统一转换属姓名 (如驼峰命名法)

    +(NSString *)mj_replacedKeyFromPropertyName121:(NSString *)propertyName

    {

        // nickName -> nick_name

        return[propertyName mj_underlineFromCamel];

    }

    [Model mj_objectWithKeyValues:dic];

    过滤字典中的值

    -(id)mj_newValueFromOldValue:(id)oldValue property(MJProperty *)property {

    if(property.type.typeClass ==[NSDate class]){

    //oldValue转换成NSDate返回

    }

    }

    还要注意点是在Mantle中遵循MTLJSONSerializing协议就遵循了NSCoding,而在MJExtension中需要手动。同时MJExtension提供了相关宏MJCodingImplementation 在基类中使用即可。

    关于MJExtension的使用在github中有详细描述,本文仅作简单记录。周末进行框架的进一步学习

    相关文章

      网友评论

          本文标题:Mantle库替换为MJExtension

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