美文网首页
iOS第三方库- MJExtension的使用

iOS第三方库- MJExtension的使用

作者: 月沉眠love | 来源:发表于2019-06-14 18:17 被阅读0次

iOS第三方库- MJExtension的使用

下载:https://github.com/CoderMJLee/MJExtension

用两个类说明PersonModel,Student;在下面有展示

通过字典->生成model

+ (instancetype)mj_objectWithKeyValues:(id)keyValues

1.字典里有Student对象

    NSDictionary *dic = @{
                          @"name":@"李四",
                          @"age":@"19",
                          @"birthday":@"1560504379",
                          @"student" : @{
                                    @"name":@"张三",
                                    @"age" :@"18",
                                    }
                          };
    
    PersonModel *p1 = [PersonModel mj_objectWithKeyValues:dic];
    NSLog(@"name=%@",p1.name);
    NSLog(@"name=%@",p1.personName);
    NSLog(@"birthday=%@",p1.birthday);
    NSLog(@"student--name--%@",p1.student.name);

输出结果:


屏幕快照 2019-06-14 下午6.18.40.png

2.有一个数组,数组里边,装的就是Student对象

 NSDictionary *dic2 = @{
                           @"area":@"安徽",
                           @"studentArray":@[@{@"name":@"小明",@"age":@"15"},
                                             @{@"name":@"小画",@"age":@"16"}]
                           };
    
    PersonModel *p2 = [PersonModel mj_objectWithKeyValues:dic2];
    
    for (Student *stu in p2.studentArray) {
        NSLog(@"---p2 name--%@",stu.name);
    }

输出结果:


屏幕快照 2019-06-14 下午6.14.29.png

pragma mark - 字典数组 -> 模型数组

+ (NSMutableArray *)mj_objectArrayWithKeyValuesArray:(NSArray *)keyValuesArray
 NSDictionary *dic = @{
                           @"area":@"安徽",
                           @"studentArray":@[@{@"name":@"小明",@"age":@"15"},
                                             @{@"name":@"小画",@"age":@"16"}]
                           };
    NSArray <PersonModel*>*resultArray = [PersonModel mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"studentArray"]];
    
    NSLog(@"resultArray--%@",resultArray);
    NSLog(@"%@",resultArray[0].name);

输出结果:


屏幕快照 2019-06-14 下午6.17.16.png

1.PersonModel.h

#import <Foundation/Foundation.h>
#import "MJExtenSion.h"
#import "Student.h"


@interface PersonModel : NSObject
@property (nonatomic, strong) NSString *name;
@property(nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSString *personName;
@property (nonatomic, strong) NSString *birthday;
@property (nonatomic, strong) Student *student;
@property (nonatomic, strong) NSArray *studentArray;

@end

2.PersonModel.m

#import "PersonModel.h"

@implementation PersonModel
//请求返回的key是name,但是,person里边,我就是想用personName,可以这么来
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{@"personName":@"name"};
    //前边的是你想用的key,后边的是返回的key(需要替换的Key)
}


//返回的是个时间戳,但是你想在生成model的时候,就转化为你想要的格式
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property{
    if ([property.name isEqualToString:@"birthday"]) {
        if (oldValue) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
            formatter.timeStyle = NSDateFormatterShortStyle;
            formatter.dateStyle = NSDateFormatterMediumStyle;
            formatter.dateFormat = @"yyyy年MM月dd日 HH:mm";
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:[oldValue doubleValue]];
            NSString *dateString = [formatter stringFromDate:date];
            return dateString;
        }
    }
    else {
        NSLog(@"日期有误");
    }
    return oldValue;
}

//有一个数组,数组里边,装的就是Student对象
+(NSDictionary *)mj_objectClassInArray{
    return @{@"studentArray":@"Student"};//前边,是属性数组的名字,后边就是类名
}


@end

3.Student.h

#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, copy) NSString *name;
@property(nonatomic, assign) NSInteger age;
@end

4.Student.m没东西了

相关文章

网友评论

      本文标题:iOS第三方库- MJExtension的使用

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