MJExtension用法

1.Person模型嵌套Book模型

@interface Book : NSObject

@property (nonatomic, copy) NSString *a;

@property (nonatomic, copy) NSString *b;

@end

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *age;

@property (nonatomic, strong) Book *book;

@end

person.m 文件

+ (NSDictionary *)mj_replacedKeyFromPropertyName{

    /* 返回的字典,key为模型属性名,value为转化的字典的多级key */

    return @{

            @"book" : @"Book"

            };

}

例子:

NSDictionary *dic = @{@"name":@"lu",

                          @"age":@"12",

                          @"Book":@{@"a":@"1",

                                    @"b":@"2"}

                          };

    Person *p =[Person mj_objectWithKeyValues:dic];

    NSLog(@"%@-%@-%@-%@",p.name,p.age,p.book.a,p.book.b);

=====================================================

2.模型套着数组,数组里面又套着模型

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *age;

@property(nonatomic,strong)NSArray *books;

@implementation Person

+(NSDictionary *)mj_objectClassInArray{


    return @{

            @"books" :@"Book"

            };

}

@interface Book : NSObject

@property (nonatomic, copy) NSString *a;

@property (nonatomic, copy) NSString *b;

@end

例子

NSDictionary *dic = @{@"name":@"lu",

                          @"age":@"12",

                          @"books":@[@{@"a":@"1",@"b":@"11"},

                                    @{@"a":@"2",@"b":@"22"}

                                    ]

                          };

    Person *p =[Person mj_objectWithKeyValues:dic];

    NSArray *books = p.books;

    for (Book *book in books) {


        NSLog(@"%@---%@",book.a,book.b);

    }

你可能感兴趣的:(MJExtension用法)