0

嗨—我正在使用 RestKit 来映射我已经收到(我已经验证正在发生)的本地 JSON 数据(Twitter 提要),并且在映射发生时遇到了问题。我收到的错误是:

2013-05-26 17:23:57.541 FoodTrucks[25932:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSEntityDescription 0x7663950> valueForUndefinedKey:]: this class is not key value coding-compliant for the key tweet.'

我的日志 (Gist)看来,它似乎正在从我的 JSON 中找到可映射的值(我打开了 RestKit 的 ObjectMapping 和 CoreData 日志记录)。我在网上看了一堆试图找出它为什么会收到这个错误,但似乎找不到任何适用于我的情况的东西。这就是我执行映射的方式:

-(void)performMapping
{
    RKEntityMapping *mapping = [ObjectMappings FoodTruckArticleMapping];
    RKManagedObjectStore *store = [[FoodTruckDataModel sharedDataModel] objectStore];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FoodTruck" inManagedObjectContext:store.mainQueueManagedObjectContext];
    RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];
    mappingDS.operationQueue = [NSOperationQueue new];
    RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:self.moreStatuses destinationObject:entity mapping:mapping];
    operation.dataSource = mappingDS;
    NSError *error = nil;
    [operation performMapping:&error];
    [mappingDS.operationQueue waitUntilAllOperationsAreFinished];
}

我也不确定这个执行映射的类是否需要特别继承任何东西,和/或它是否需要可能采用RKMappingOperationDelegate. 现在,它只是继承自NSObject. 这是我的映射类的外观:

对象映射.h:

@interface ObjectMappings : RKEntityMapping

+(RKEntityMapping *)FoodTruckArticleMapping;

@end

对象映射.m:

@implementation ObjectMappings

+(RKEntityMapping *)FoodTruckArticleMapping
{
    RKEntityMapping *jsonMapping = [RKEntityMapping mappingForEntityForName:@"FoodTruck" inManagedObjectStore:[[FoodTruckDataModel sharedDataModel] objectStore]];
    jsonMapping.identificationAttributes = @[@"tweetID"];

    [jsonMapping addAttributeMappingsFromDictionary:@{
 @"text": @"tweet", @"user.screen_name": @"foodTruckName", @"id_str": @"tweetID", @"created_at": @"timeStamp"}];

    return jsonMapping;
}

@end

我的对象类是一个NSManagedObject包含所有@dynamic方法的实现。任何帮助将不胜感激!

编辑:

NSManagedObject类,FoodTruck.h:

@interface FoodTruck : NSManagedObject

@property (nonatomic, retain) NSString *foodTruckName;
@property (nonatomic, retain) NSString *tweet;
@property (nonatomic, retain) NSDate *timeStamp;
@property (nonatomic, retain) NSString *tweetID;

@end

食品卡车.m

@implementation FoodTruck

@dynamic foodTruckName;
@dynamic tweet;
@dynamic timeStamp;
@dynamic tweetID;

@end
4

1 回答 1

0

我最终发现了我的问题——我的错误是没有创建一个用我的实体初始化的 NSManagedObject 的新实例:

-(void)performMapping

NSManagedObject *newManagedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:store.persistentStoreManagedObjectContext];

然后 NSManagedObject 成为 RKMappingOperation 中的目标对象,而不是我的实体,就像我之前所做的那样:

RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:self.moreStatuses destinationObject:newManagedObject mapping:mapping];

还需要-(BOOL)saveToPersistentStore:(NSError **)error在 RKManagedObjectStore 上调用 : 以将其保存到 SQLite 数据库:

[store.persistentStoreManagedObjectContext saveToPersistentStore:&error];

那是我花了一段时间才弄清楚的另一件事!

于 2013-05-31T15:55:48.103 回答