实体(null)不是密钥值编码兼容的密钥"标题"

Sea*_*ess 5 core-data objective-c ios mogenerator restkit

我试图让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".'
Run Code Online (Sandbox Code Playgroud)

在我看来,它成功地找到了我的Book类,并且它有一个title属性.我究竟做错了什么?


Books.xcdatamodel

Book
  title: String
Run Code Online (Sandbox Code Playgroud)

我有一个url at localhost:3000/books/initial返回以下(JSON)

[{title:"one"}, {title:"two"}]
Run Code Online (Sandbox Code Playgroud)

我正在使用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);
}];
Run Code Online (Sandbox Code Playgroud)

编辑://Send RequestRKTwitterCoreData应用程序中找到的部分之前添加了以下行,但我仍然得到相同的错误

// Other Initialization (move this to app delegate)
[objectStore createPersistentStoreCoordinator];
[objectStore createManagedObjectContexts];

objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext];
Run Code Online (Sandbox Code Playgroud)

Sea*_*ess 4

问题是映射中的路径不正确。我有http://localhost:3000/我的领域,它应该在的地方http://localhost:3000,我有我的books/initial/路径,它应该在的地方/books/initial/

\n\n

请参阅RestKit 0.20 \xe2\x80\x94 CoreData:错误:无法在 NSManagedObject 类上调用指定的初始值设定项

\n\n

我还忘记创建持久存储。这是完整的工作示例:

\n\n
// Core Data Example\n// Initialize RestKIT\nRKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]];\nRKManagedObjectStore* objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:self.model];\nobjectManager.managedObjectStore = objectStore;\n\n// Mappings\nRKEntityMapping *bookMapping = [RKEntityMapping mappingForEntityForName:@"Book" inManagedObjectStore:objectStore];\n[bookMapping addAttributeMappingsFromArray:@[@"title"]];\n\nRKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bookMapping pathPattern:@"/books/initial/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];\n\n[objectManager addResponseDescriptor:responseDescriptor];\n\n// Other Initialization (move this to app delegate)\n[objectStore createPersistentStoreCoordinator];\n\nNSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKTwitter.sqlite"];\nNSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];\nNSError *error;\nNSPersistentStore *persistentStore = [objectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error];\nNSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);\n\n[objectStore createManagedObjectContexts];\n\nobjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext];\n\n// Send Request\n[objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) {\n    NSLog(@"SUCCESS");\n} failure: ^(RKObjectRequestOperation * operation, NSError * error) {\n    NSLog(@"FAILURE %@", error);\n}];\n
Run Code Online (Sandbox Code Playgroud)\n