实体(null)不是密钥值编码兼容的关键“标题”

我正在尝试让RestKit和CoreData一起工作。 我越来越近,但我收到以下错误:

CoreData: error: Failed to call designated initializer on NSManagedObject class 'Book' *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "title".' 

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


Books.xcdatamodel

 Book title: String 

我在localhost:3000/books/initial有一个url 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]; 

问题是映射中的路径不正确。 我有http://localhost:3000/作为我的域名,它应该是http://localhost:3000 ,我有books/initial/作为应该是/books/initial/的路径。

请参阅RestKit 0.20 – CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序

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

 // Core Data Example // Initialize RestKIT 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]; // Other Initialization (move this to app delegate) [objectStore createPersistentStoreCoordinator]; NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"RKTwitter.sqlite"]; NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"]; NSError *error; NSPersistentStore *persistentStore = [objectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error]; NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); [objectStore createManagedObjectContexts]; objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:objectStore.persistentStoreManagedObjectContext]; // Send Request [objectManager getObjectsAtPath:@"/books/initial/" parameters:nil success:^(RKObjectRequestOperation * operation, RKMappingResult *mappingResult) { NSLog(@"SUCCESS"); } failure: ^(RKObjectRequestOperation * operation, NSError * error) { NSLog(@"FAILURE %@", error); }]; 

我没有看到你添加持久存储的位置。 你忘记这样做了吗?