Restkit:RKValueTransformer transformationBlock没有调用

时间:2014-12-27 19:31:22

标签: ios restkit restkit-0.20

这就是我所拥有的:

在我的JSON响应中我有一系列选项: Options : ["red","green", "blue"]

我想将其转换为字符串:options = @"red, green, blue";(我已将menuComponent实体的options属性定义为'NSString')

我为该属性提供了一个customValueTransformer,但是不会调用transformationBlock。 有没有人知道我做错了什么?

RKEntityMapping *menuComponentMapping = [RKEntityMapping mappingForEntityForName:@"MenuComponent" inManagedObjectStore:managedObjectStore];

menuComponentMapping.identificationAttributes = @[@"menuComponentID"];
[menuComponentMapping addAttributeMappingsFromDictionary:@{
                                                           @"ID":@"menuComponentID",
                                                           @"Name":@"name",
                                                           @"Description":@"descriptions",
                                                           @"Thumbnail":@"thumbnail"
                                                           }];
//every menu component have some options
RKAttributeMapping *optionsMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"Options" toKeyPath:@"options"];

//converting options to string
RKValueTransformer *arrayToStringTransformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class sourceClass, __unsafe_unretained Class destinationClass) {
    // I don't know check what with what!
    return ([sourceClass isSubclassOfClass:[NSNumber class]] && [destinationClass isSubclassOfClass:[NSString class]]);
} transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, Class outputValueClass, NSError *__autoreleasing *error) {
    // Validate the input and output

    // Perform the transformation
// iterating through options and append to a string

    return YES;
}];

//adding custom value transformer
optionsMapping.valueTransformer = arrayToStringTransformer;
//adding optionsMapping
[menuComponentMapping addPropertyMapping:optionsMapping];
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:arrayToStringTransformer atIndex:0];

RKRelationshipMapping *menuComponentRelationShip = [RKRelationshipMapping relationshipMappingFromKeyPath:@"MenuComponents" toKeyPath:@"children" withMapping:menuComponentMapping];

//adding menucomponent children relationship to menuComponent
[menuComponentMapping addPropertyMapping:menuComponentRelationShip];

return menuComponentMapping;
}

1 个答案:

答案 0 :(得分:0)

您的代码甚至会将此部分视为一个问题:

// I don't know check what with what!
return ([sourceClass isSubclassOfClass:[NSNumber class]] && [destinationClass isSubclassOfClass:[NSString class]]);

因为您没有尝试从NSNumber转换,所以您尝试从NSArray进行转换。如果类类型不匹配,则不使用变换器。

如果未调用该块,则问题在于变压器注册。当变换器直接提供给映射但你没有显示如何使用映射时,这不应该发生......