user.h
objc
#import <Mantle/Mantle.h> @interface user : MTLModel <MTLJSONSerializing> @property (nonatomic, strong) NSNumber *userId; @property (nonatomic, strong) NSNumber *createdTime; @property (nonatomic, strong) NSNumber *startIndex; @property (nonatomic, strong) NSNumber *endIndex; @property (nonatomic, strong) NSNumber *stype; @property (nonatomic, assign, readonly) BOOL isDeleted; @property (nonatomic, strong) NSString *phone; @property (nonatomic, strong) NSString *password; @end
user.m
objc
#import "user.h" @implementation user + (NSDictionary *)JSONKeyPathsByPropertyKey { return @{@"userId":@"id"}; } - (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError *__autoreleasing *)error { self = [super initWithDictionary:dictionaryValue error:error]; if (self == nil) return nil; // Store a value that needs to be determined locally upon initialization. // _retrievedAt = [NSDate date]; return self; } @end
调用
DHUser *user = [[DHUser alloc]initWithDictionary:[response objectForKey:@"user"] error:&error];
执行以后
产出的效果有以下问题
1.userId没能够成功对应id
2.我没有建立headPic,不需要,但是强制要求。
解决了,粗心了,初始化DHUser *user = [[DHUser alloc]initWithDictionary:[response objectForKey:@"user"] error:&error];
修改为DHUser *user = [MTLJSONAdapter modelOfClass:DHUser.class fromJSONDictionary:[response objectForKey:@"user"] error:&error];
使用MTLJSONAdapter
进行声明。