zoukankan      html  css  js  c++  java
  • MJExtension的使用

    1. Plist → 模型数组

    控制器中引用#import "MJExtension.h"

     模型数组 = [模型类名 objectArrayWithFilename:@"文件名.plist"];

    2. 对NSLog的优化,解决 调试时,打印模型,只打印出内存地址的问题

    使用方法:在模型类的.m文件中,引用#import "MJExtension.h"

    在@implementation 和 @end之间,写上MJLogAllIvrs

    3. 对NSCoding的优化,不用再写繁琐的解档和归档方法了

    使用方法:在模型类的.m文件中,引用#import "MJExtension.h"

    在@implementation 和 @end之间,写上MJCodingImplementation

    4. 字典数组 → 模型数组

    4.1 场景一: [  字典1,

            字典2,

            字典3  ]

    如果每个字典都是一个模型,可以用

    NSArray *modelArray = [模型类名 objectArrayWithKeyValuesArray:字段数组];

    4.2 场景二:在场景一的基础上,每个字典里面,有数组(假设数组的key值是arrayName),数组里面存放着若干个相同的模型,使用下面的方法

    使用方法:

    首先在模型类.m文件中,引入#import "MJExtension.h"

    然后在 @implementation 和 @end之间 写上

    - (NSDictionary *)objectClassInArray

    {

        return @{@"arrayName" : [模型类名 class]};

    }

    - (NSDictionary *)mj_objectClassInArray{

        return @{@"labelModels" : @"IdleReleaseLabModel"};

    }

     5. 如果 服务器传过来的 字典数组里的字典的Key,是OC里的关键字,而使用MJExtention的前提是,模型里的属性名和数组的key一致才行(区分大小写),怎么办?

    使用replacedKeyFromPropertyName

    使用方法:1.在模型类.m文件引入"MJExtension.h"

        2.实现方法

    + (NSDictionary *)replacedKeyFromPropertyName

    {

        return @{@“非关键字的属性名” : @“数组的key”};

    }

    6. 单个字典 → 单个模型

    + (instancetype)objectWithKeyValues:(NSDictionary *)keyValues

    //1.使用MJExtension的时候属性名不识别解决方法,假如id编PID
    -(NSDictionary *)replacedKeyFromPropertyName{
        return @{@"PID":@"id"};
    }
    //2.当一个类中包含多个对象
    - (NSDictionary *)objectClassInArray
    {
        return @{@"regions" : [Class class]};
    }
    //3.MJCodingImplementation 再.m文件中加入此句,代表带向序列化


    //当屏幕尺寸放生旋转是调用
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    //字典转模型类  objectWithKeyValues
    self.deal = [MTDeal objectWithKeyValues:[result[@"deals"] firstObject]];

    字典转json:
    -(NSString*)DataTOjsonString:(id)object
    
    {
    
        NSString *jsonString = nil;
    
        NSError *error;
    
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
    
                                                           options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
    
                                                             error:&error];
    
        if (! jsonData) {
    
            NSLog(@"Got an error: %@", error);
    
        } else {
    
            jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
        }
    
        
    
        NSLog(@"-----------%@",jsonString);
    
        return jsonString;
    
    }
    

    本地的json转成字典

    NSString *path = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"json"];
                     NSData *JSONData = [NSData dataWithContentsOfFile:path];
                     NSArray *jsonArr =  [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments error:nil];
    

    NSDate

    - (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
    {
        if ([property.name isEqualToString:@"publisher"]) {
            if (oldValue == nil) return @"";
        } else if (property.type.typeClass == [NSDate class]) {
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd";
            return [fmt dateFromString:oldValue];
        }
    
        return oldValue;
    }
    @end
    
    https://github.com/CoderMJLee/MJExtension#Model_JSON 
  • 相关阅读:
    spring in action小结4.1
    spring in action小结3 运行时值注入
    python-__init__.py 与模块对象的关系
    Python-常用库扩展
    Qt-优化布局结构
    Python-文件修改器
    C语言-数据结构(一)
    Python-PyQt4学习笔记
    Python-PyQt4学习资料汇总
    Linux-查看C语言手册及man的特殊用法
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/5034874.html
Copyright © 2011-2022 走看看