![](/img/trans.png)
[英]Mapping with Restkit 0.20 this class is not key value coding-compliant for the key
[英]RestKit 0.20.1 mapping local JSON “this class is not key value coding-compliant for the key…”
嗨,我正在使用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
繼承而來。 這是我的映射類的外觀:
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
我的對象類是一個具有所有@dynamic
方法實現的NSManagedObject
。 任何幫助將不勝感激!
編輯:
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
食物卡車
@implementation FoodTruck
@dynamic foodTruckName;
@dynamic tweet;
@dynamic timeStamp;
@dynamic tweetID;
@end
我最終發現了我的問題-我的錯誤不是創建一個用我的實體初始化的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
上調用-(BOOL)saveToPersistentStore:(NSError **)error
,以將其保存到SQLite數據庫:
[store.persistentStoreManagedObjectContext saveToPersistentStore:&error];
那是我花了一段時間才弄清楚的另一件事!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.