我使用Restkit 0.26.0来映射多层的
JSON,这个关键路径会导致麻烦:
productColorImages: [
{
id: 10,
productId: "232",
color: "green",
url: "exampleURL.com"
},
{
id: 11,
productId: "232",
color: "red",
url: "exampleURL.com"
},
{
id: 12,
productId: "232",
color: "blue",
url: "exampleURL.com"
}
],
我的映射
RKObjectMapping *transferProductColorImageMapping = [RKObjectMapping mappingForClass:[TransferProductColorImage class]];
[transferProductColorImageMapping addAttributeMappingsFromDictionary:@{ @"identifier" : @"id",
@"URL" : @"URL",
@"productId" : @"productId",
@"color" : @"color"
[getProductPageMapping addRelationshipMappingWithSourceKeyPath:@“productColorImages”mapping:transferProductColorImageMapping];
目的地类
@interface TransferProductColorImage : NSObject
@property (nonatomic) NSNumber * identifier; // Mapps to id ;int 64
@property (nonatomic) NSString * URL;
@property (nonatomic) NSString * productId;
@property (nonatomic) NSString * color;
问题是生成的对象属于正确的类,但属性未填充.
我在日志中搜索并发现了这个:
2016-05-30 17:42:24.007 thebrandsapp[20697:7047455] T restkit.object_mapping:RKMappingOperation.m:1173 Performing mapping operation: <RKMappingOperation 0x7f9e3b9c11e0> for 'TransferProductColorImage' object. Mapping values from object {
color = green;
id = 10;
productId = 232;
url = "exampleURL.com";
} to object <TransferProductColorImage: 0x7f9e3b846d60> with object mapping (null)
在Model对象上,productColorImages是一个NSArray:
@property (nonatomic) NSArray <TransferProductColorImage *> * productColorImages;
这很奇怪,因为.如果映射为null,Restkit如何知道要使用的映射.是否有任何理由可以将对象映射设置为null?
更新:我发现标识符和颜色属性已正确映射,标识符和网址未正确映射.
最佳答案 我将URL属性更改为小写:“url”并将其映射为与之前完全相同且有效.似乎全帽属性名称抛出了RestKit.
我突然识别了标识符和id,现在它们也正确映射.