错误消息如下所示:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Channel 0x7fb34b5d1420> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key user.'
这是代码:
NSDictionary *json = @{@"u1" : @{@"handle" : @"h1", @"authToken" : @"123"}};
NSError *e = nil;
Channel *c = [[Channel alloc] initWithDictionary:json error:&e];
NSLog(@"channel.u1.handle: %@", c.u1.handle);
Channel.m
:
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"u1" : @"u1"
};
}
+ (NSValueTransformer *)u1Transformer {
return [MTLJSONAdapter dictionaryTransformerWithModelClass:[User class]];
}
User.m
:
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"handle" : @"handle",
@"authToken" : @"token"
};
}
如何将我的 JSON 解析为可可对象?