RestKit value transformer validation block not called

五迷三道 提交于 2019-12-11 05:27:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!