RestKit 0.20.1映射本地JSON“这个类不是密钥值编码兼容的密钥......”

时间:2013-05-27 00:00:28

标签: ios objective-c json core-data restkit

嗨 - 我使用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.'

my log (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。这就是我的映射类的外观:

ObjectMappings.h:

@interface ObjectMappings : RKEntityMapping

+(RKEntityMapping *)FoodTruckArticleMapping;

@end

ObjectMappings.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

FoodTruck.m

@implementation FoodTruck

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

@end

1 个答案:

答案 0 :(得分: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];

这是我需要一段时间来解决的问题!