【问题标题】:How do I identify the cause of a "no attribute was found" exception?如何确定“未找到属性”异常的原因?
【发布时间】: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 属性。谢谢!将其添加为答案,以便我可以给你积分。

标签: ios ios6 restkit


【解决方案1】:

id 属性添加到您的 Pool 实体。
注意: 将属性命名为 id 会起作用,但我建议将其更改为非目标-C 关键字。

【讨论】:

    猜你喜欢
    • 2014-01-17
    • 2017-10-13
    • 2021-11-05
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2012-11-06
    相关资源
    最近更新 更多