【发布时间】:2013-05-14 00:55:21
【问题描述】:
我按照 RestKit 文档中的所有步骤将其添加到项目中,但是当我尝试启动我的应用程序时,它在 application:application didFinishLaunchingWithOptions: 中的这一行停止:
entityMapping.identificationAttributes = @[@"_id"];
之前的代码是:
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MyModel" ofType:@"momd"]];
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
// Init the Core Data stack
[managedObjectStore createPersistentStoreCoordinator];
NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
NSAssert(persistentStore, @"Faild to add persistent store: %@", error);
[managedObjectStore createManagedObjectContexts];
// Set the default shared store instance
[RKManagedObjectStore setDefaultStore:managedObjectStore];
// Configure the object manager
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:9090"]];
objectManager.managedObjectStore = managedObjectStore;
[RKObjectManager setSharedManager:objectManager];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Pool" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromDictionary:@{
@"id": @"_id",
@"about_url": @"aboutURL",
@"closed": @"closed",
@"created_on": @"createdOn"
}];
entityMapping.identificationAttributes = @[@"_id"];
异常在 RKEntityMapping.m 第 100 行 (if (!attribute) [NSException raise:NSInvalidArgumentException format:@"Invalid attribute '%@': no attribute was found for the given name in the '%@' entity.", attributeOrName, [entity name]];) 中被抛出,它是 RKArrayOfAttributesForEntityFromAttributesOrNames 的一部分。
当我po [entity name] 时,它显示“Pool”,但我的 .xcdatamodeld 中确实有一个 Pool 实体。
我还可以在哪里查找此错误的原因?
【问题讨论】:
-
为什么是
@[@"_id"]而不是@[@"id"]? -
我最初有 @[@"id"] 但 JSON 中的键是 '_id' 所以我认为这可能是问题所在并更改了该行。
-
Pool实体中的id属性名称是什么? -
就是这样。我在 Pool 实体中没有 id 属性。谢谢!将其添加为答案,以便我可以给你积分。