嗨—我正在使用 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