1
這是我有:Restkit:RKValueTransformer transformationBlock不叫
在我的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;
}
我有一個restaurantEntity與menuComponentEntity有關係。在我的responseDescriptor中,我提供了我的restaurantMapping和一個webApi url。 restaurantManager在沒有任何問題的情況下加載所有數據。選項'valueTransformerWithValidationBlock'調用兩次:第一次目的地是NSString類,sourceClass是NCSFString類,第二次destinationClass是NSNumber類,源類是'NSCFNumber'類,但轉換類不被調用。如果例如將自定義值轉換器添加到另一個屬性(如「name」),則會調用變換塊! – 2014-12-28 19:46:56
打開跟蹤日誌記錄。數據模型中設置了哪種數據類型?而託管對象類是自動生成的? – Wain 2014-12-28 22:45:28