KWMRequestResult.m 1.47 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  KWMRequestResult.m
//  iLiJiPao
//
//  Created by kevin on 4/27/15.
//  Copyright (c) 2015 kollway. All rights reserved.
//

#import "KWMRequestResult.h"

@implementation KWMRequestResult{
    
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
                        modelClass:(Class)modelClass
                             error:(NSError **)err {
lee committed
18
    self = [super initWithDictionary:@{@"message":@"",@"code":@"",@"data":dict} error:err];
houweibin committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    if (self) {
        NSDictionary *dataDictionary = dict[@"data"];
        if(dataDictionary){
            self.data = [self buildData:modelClass jsonDictionary:dataDictionary];
        }
    }
    
    return self;
}

- (id)buildData:(Class)modelClass jsonDictionary:(NSDictionary *)jsonDictionary {
    id result = nil;
    
    if(modelClass != nil){
        NSError *error;
        id tmpResult = [[modelClass alloc] initWithDictionary:jsonDictionary error:&error];
        if(error){
            NSLog(@"buildData error: %@", [error localizedDescription]);
        }else{
            result = tmpResult;
        }
    }
    
    return result;
}

+ (JSONKeyMapper *)keyMapper {
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"code" : @"code",
                                                       @"message" : @"message",
                                                       @"page_size" : @"pageSize"
                                                       }];
}

@end