问题
I'm using RestKit (the latest version 0.23.1), and I have defined a custom transformer between a string value to a number field as follows:
mapping.valueTransformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class inputValueClass, __unsafe_unretained Class outputValueClass) {
return [inputValueClass isSubclassOfClass:[NSString class]];
} transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputClass, NSError *__autoreleasing *error) {
if (![inputValue isKindOfClass:[NSString class]]) {
return NO;
}
// conversion logic follows...
return YES;
}];
The transformer works properly, however the validation block is not being called (I've set a breakpoint to verify that), which forces me to check in the transformationBlock that the inputValue is indeed an instance of NSString.
Why is the validation block not being invoked, what am I doing wrong? I couldn't find any issue on this in RestKit GitHub page.
回答1:
You are setting the transformer directly onto the mapping and as the only transformer so it is assumed that it always applies.
You want to set the transformer to a RKCompoundValueTransformer
(Using defaultValueTransformer
And your custom transformer) as it does perform validation before applying transformations.
来源:https://stackoverflow.com/questions/24476717/restkit-value-transformer-validation-block-not-called