我试图让 RestKit 和 CoreData 一起工作。我越来越近了,但我收到以下错误:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Book'
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<Book 0x8454560> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "title".'
在我看来,它成功地找到了我的 Book 类,并且它确实具有 title 属性。我究竟做错了什么?
书籍.xcdatamodel
Book
title: String
localhost:3000/books/initial
我有一个返回以下(JSON)的网址
[{title:"one"}, {title:"two"}]
我正在使用 mogenerator 创建我的课程。我没有添加任何东西Book
,但_Book
显然已经定义了 title 属性。
最后,这是我用来加载请求的代码。
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/"]];
RKManagedObjectStore* objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.model];
objectManager.managedObjectStore = objectStore;
// Mappings
RKEntityMapping *bookMapping = [RKEntityMapping mappingForEntityForName:@"Book" inManagedObjectStore:objectStore];
[bookMapping addAttributeMappingsFromArray:@[@"title"]];
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bookMapping pathPattern:@"books/initial/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
// Send Request
[objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) {
NSLog(@"SUCCESS");
} failure: ^(RKObjectRequestOperation * operation, NSError * error) {
NSLog(@"FAILURE %@", error);
}];
编辑://Send Request
我在应用程序中找到的部件之前添加了以下几行RKTwitterCoreData
,但我仍然得到同样的错误
// Other Initialization (move this to app delegate)
[objectStore createPersistentStoreCoordinator];
[objectStore createManagedObjectContexts];
objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext];