user.h
#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
#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进行声明。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。