我正在使用该Mantle
框架,并且在将某些值序列化为MTLModel
. 这是JSON
我从服务器收到的:
{
"id":50,
"name":"UserName",
"email":"user@username.com",
"profile":{
"picture": {
"original": "http://original.com/picture",
"versions": {
"thumb": "http://thumb.com/picture",
"small": "http://small.com/picture"
}
}
}
}
我MTLModel
按以下方式设置了我的 s:
用户
@interface User : MTLModel <MTLJSONSerializing>
@property (nonatomic, readonly) NSNumber *id;
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *email;
@property (nonatomic) Profile *profile;
@end
@implementation User
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"id": @"id",
@"name": @"name",
@"email": @"email",
@"profile": @"profile"
};
}
- (NSValueTransformer *)profileJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *profileDict) {
return [MTLJSONAdapter modelOfClass:Profile.class
fromJSONDictionary:profileDict
error:nil];
} reverseBlock:^(Profile *profile) {
return [MTLJSONAdapter JSONDictionaryFromModel:profile];
}];
}
@end
轮廓
@interface Profile : MTLModel <MTLJSONSerializing>
@property (nonatomic) Picture *picture;
@end
@implementation Profile
+ (NSDictionary *)JSONKeyPathsByPropertyKeys {
return @{
@"picture": @"picture"
};
}
- (NSValueTransformer *)pictureJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *picDict) {
return [MTLJSONAdapter modelOfClass:Picture.class
fromJSONDictionary:picDict
error:nil];
} reverseBlock:^(Picture *picture) {
return [MTLJSONAdapter JSONDictionaryFromModel:picture];
}];
}
@end
图片
@interface Picture : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *original;
@property (nonatomic) Versions *versions;
@end
@implementation Picture
+ (NSDictionary *)JSONKeyPathsByPropertyKeys {
return @{
@"original": @"original",
@"versions": @"versions"
};
}
- (NSValueTransformer *)versionsJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *versionDict) {
return [MTLJSONAdapter modelOfClass:Versions.class
fromJSONDictionary:versionDict
error:nil];
} reverseBlock:^(Versions *versions) {
return [MTLJSONAdapter JSONDictionaryFromModel:versions];
}];
}
@end
版本
@interface Versions : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *thumb;
@property (nonatomic) NSString *small;
@end
@implementation Versions
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"thumb": @"thumb",
@"small": @"small"
};
}
@end
我正在执行以下Overcoat
POST 调用来获取 JSON。当我NSLog
响应时,我收到了 JSON 罚款:
[[Client getInstance] POST:@"authorize.json" parameters:params completion:^(id response, NSError *error) {
if (!error) {
User *user = [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:[response result] error:nil];
Picture *picture = [[user profile] picture];
NSLog(@"%@", picture);
} else {
NSLog(@"%@", error);
}
}];
当我尝试Picture
像这样抓取对象时会出现问题:
Picture *picture = [[user profile] picture];
我得到一个例外:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary picture]: unrecognized selector sent to instance 0x7f95026d4210'
我怎样才能解决这个问题?我究竟做错了什么?