使用restkit解析对象时出错

时间:2014-04-07 08:54:30

标签: ios restkit restkit-0.20

我使用此代码使用restkit解析对象

RKObjectMapping* questionMapping = [RKObjectMapping mappingForClass:[Questions class]];
[questionMapping addAttributeMappingsFromArray:[[NSArray alloc] initWithObjects:@"questionId",@"questionName" , @"answer1", @"answer2" , @"answer3" , @"answer4" , nil]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:questionMapping method:RKRequestMethodAny pathPattern:@"/level" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

//[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

NSURL *URL = [NSURL URLWithString:@"http://192.168.1.106:8080/level?gameId=24"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
    questions = mappingResult.array ;
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

[objectRequestOperation start];

但我总是得到错误

2014-04-07 10:52:54.890 Matching[1226:1303] CoreData: error: Failed to call     designated initializer on NSManagedObject class 'Questions' 
2014-04-07 10:52:54.895 Matching[1226:1303] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Questions 0x167322c0> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "questionId".'
*** First throw call stack:
(0x31116f4b 0x3b4a66af 0x31116c61 0x30ee8531 0x31a37da7 0x52cb7 0x54339 0x5552d 0x5c165 0x31a457db 0x46937 0x46053 0x3110b3ef 0x310ac443 0x45dc9 0x47a3f 0x482d3 0x48beb 0x31a457db 0xbca97 0xbb0a5 0x31a457db 0x31ae9995 0x3b98a293 0x3b98a6f7 0x3b99c8f9 0x3b99cb79 0x3bacbdbf 0x3bacbc84)
libc++abi.dylib: terminating with uncaught exception of type NSException
你觉得这个问题怎么样?

1 个答案:

答案 0 :(得分:0)

如果Questions是托管对象,则无法使用:

RKObjectMapping* questionMapping = [RKObjectMapping mappingForClass:[Questions class]];

因为您需要创建RKEntityMapping的实例并为其提供实体名称和对托管对象库的引用。完成后,您需要将RKObjectRequestOperation更改为RKManagedObjectRequestOperation的实例,以便它适当地处理核心数据集成。