Restkit - 实体(空)不符合键“行”的键值编码
Posted
技术标签:
【中文标题】Restkit - 实体(空)不符合键“行”的键值编码【英文标题】:Restkit - the entity (null) is not key value coding-compliant for the key "rows" 【发布时间】:2013-04-10 19:08:20 【问题描述】:我使用的是 RESTKIT 0.20,但映射有问题。谁能告诉我错误在哪里?谢谢,这是我的代码:
RKObjectMapping* pageMapping = [RKEntityMapping mappingForEntityForName:@"Page" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[pageMapping addAttributeMappingsFromArray:@[ @"rows", @"columns" ]];
RKObjectMapping* magazineMapping = [RKEntityMapping mappingForEntityForName:@"Magazine" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[magazineMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"pages"
toKeyPath:@"pages"
withMapping:pageMapping]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:magazineMapping pathPattern:nil keyPath:@"magazine" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kBasePath, [magazine contentPath]]];
NSURLRequest *r = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:r responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
failure:^(RKObjectRequestOperation *operation, NSError *error)
RKLogError(@"Operation failed with error: %@", error);
];
[objectRequestOperation start];
还有实体杂志:
@interface Magazine : NSManagedObject
@property (strong) NSString * contentPath;
@property (strong) NSDate * date;
@property (strong) NSString * icon;
@property (strong) NSString * magazineID;
@property (strong) NSDecimalNumber * price;
@property (strong) id priceLocale;
@property (strong) NSString * title;
@property (strong) Page *pages;
最后是 Page 实体:
@class Magazine;
@interface Page : NSManagedObject
@property (strong) NSString * content;
@property (strong) NSNumber * contentID;
@property (strong) NSNumber * columns;
@property (strong) NSNumber * rows;
@property (strong) NSString *pageID;
@property (strong) Magazine *assignedMagazine;
错误一定是在映射页面实体,因为其他“下载”方法没有问题。
【问题讨论】:
【参考方案1】:像这样使用映射:
RKManagedObjectMapping *mappingPage = [RKManagedObjectMapping mappingForClass:[Page class] inManagedObjectStore:self.objectStore];
mappingPage.setDefaultValueForMissingAttributes = NO;
mappingPage.primaryKeyAttribute = @"contentID";
[mappingPage mapKeyPathsToAttributes:
@"CONTENT_FROM_WEBSERVICE", @"content",
@"ID_FROM_WEBSERVICE", @"contentID",
@"COLUMNS_FROM_WEBSERVICE", @"columns",
@"ROWS_FROM_WEBSERVICE", @"rows",
nil];
【讨论】:
现在似乎是RKEntityMapping
,RK 0.20以上是关于Restkit - 实体(空)不符合键“行”的键值编码的主要内容,如果未能解决你的问题,请参考以下文章
绑定和核心数据:实体 xxx 不符合键“(null)”的键值编码