未调用 RestKit 值转换器验证块 [英] RestKit value transformer validation block not called

查看:38
本文介绍了未调用 RestKit 值转换器验证块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RestKit(最新版本 0.23.1),并且我在字符串值到数字字段之间定义了一个自定义转换器,如下所示:

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;
}];

转换器工作正常,但是没有调用验证块(我设置了一个断点来验证),这迫使我在转换块中检查 inputValue 确实是 NSString 的实例.

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.

为什么没有调用验证块,我做错了什么?我在 RestKit GitHub 页面中找不到关于此的任何问题.

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.

推荐答案

您将转换器直接设置到映射上并且作为唯一的转换器,因此假定它始终适用.

You are setting the transformer directly onto the mapping and as the only transformer so it is assumed that it always applies.

您想将转换器设置为 RKCompoundValueTransformer(使用 defaultValueTransformer 和您的自定义转换器),因为它在应用转换之前执行验证.

You want to set the transformer to a RKCompoundValueTransformer (Using defaultValueTransformer And your custom transformer) as it does perform validation before applying transformations.

这篇关于未调用 RestKit 值转换器验证块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆