实体(空)不是关键字“标题”的关键值编码兼容的。 [英] The entity (null) is not key value coding-compliant for the key "title"
问题描述
我想让RestKit和CoreData一起工作。我越来越接近,但我得到以下错误:
I'm trying to get RestKit and CoreData to work together. I'm getting closer, but I'm getting the following error:
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属性。我做错了什么?
It seems to me that it's finding my Book class successfully, and it DOES have a title property. What am I doing wrong?
Books.xcdatamodel
Books.xcdatamodel
Book
title: String
我有一个URL
I have a url at localhost:3000/books/initial
that returns the following (JSON)
[{title:"one"}, {title:"two"}]
我使用mogenerator类。我没有向 Book
添加任何内容,但是 _Book
显然已定义标题属性。
I'm using mogenerator to create my classes. I haven't added anything to Book
, but _Book
clearly has the title property defined.
最后,这里是我用来加载请求的代码。
Finally, here's the code I use to load the request.
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);
}];
EDIT:在 RKTwitterCoreData
应用程序中找到的 //发送请求
部分之前添加了以下行,同样的错误
I added the following lines right before the //Send Request
part, found in the RKTwitterCoreData
app, but I still get the same error
// 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 /
的路径。
The problem was that path was not correct in the mapping. I had http://localhost:3000/
as my domain, where it should have been http://localhost:3000
, and I had books/initial/
as the path where it should have been /books/initial/
.
请参阅 RestKit 0.20 - CoreData:错误:无法调用NSManagedObject类上的指定初始化器
我也忘了创建持久存储。下面是完整的工作示例:
I also forgot to create the persistent store. Here's the full working example:
// 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);
}];
这篇关于实体(空)不是关键字“标题”的关键值编码兼容的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!