未调用 RestKit valueTransformer [英] RestKit valueTransformer not being called

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

问题描述

我正在使用 RestKit 与我的 JSON 端点通信.端点以毫秒数"为单位返回 UNIX 时间戳.但是 RestKit 的默认转换器假定它是秒数",而我在 NSDate 中得到了错误的值.

I am using RestKit to talk to my JSON endpoints. The endpoint returns a UNIX timestamp in 'number of milliseconds'. But RestKit's default transformer assumes that it is 'number of seconds' and I get a wrong value in NSDate.

于是我环顾四周,发现我需要使用一个自定义的valueTransformer来告诉RestKit如何转换我的时间戳.这是我的代码.

So I looked around, and found that I need to use a custom valueTransformer to tell RestKit how to transform my timestamp. Here's the code I have.

+ (RKBlockValueTransformer*) timeIntervalInMillisecondsSince1970TwoWayTransformer {

    return [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class sourceClass, __unsafe_unretained Class destinationClass) {
        // This transformer handles `NSNumber` <-> `NSDate` transformations
        NSLog(@"checking new transformer");
        return (([sourceClass isSubclassOfClass:[NSNumber class]] && [destinationClass isSubclassOfClass:[NSDate class]]) ||
                ([sourceClass isSubclassOfClass:[NSDate class]] && [destinationClass isSubclassOfClass:[NSNumber class]]));
    } transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputValueClass, NSError *__autoreleasing *error) {
        NSLog(@"transforming");
        RKValueTransformerTestInputValueIsKindOfClass(inputValue, (@[ [NSNumber class], [NSDate class] ]), error);
        RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputValueClass, (@[ [NSNumber class], [NSDate class] ]), error);
        if ([outputValueClass isSubclassOfClass:[NSDate class]]) {
            if ([inputValue isKindOfClass:[NSNumber class]]) {
                *outputValue = [NSDate dateWithTimeIntervalSince1970:[inputValue doubleValue] / 1000];
            }
        } else if ([outputValueClass isSubclassOfClass:[NSNumber class]]) {
            *outputValue = @((NSInteger)[inputValue timeIntervalSince1970] * 1000);
        }
        return YES;
    }];
}

然后我将此转换器添加到 RestKit 的默认转换器中.

Then I am adding this transformer to RestKit's default transformer.

RKValueTransformer* transformer = [self timeIntervalInMillisecondsSince1970TwoWayTransformer];
[[RKValueTransformer defaultValueTransformer] addValueTransformer:transformer];

但是,我的变压器从来没有被调用过.我在里面写的 NSLog 语句永远不会被执行!

But, my transformer never gets called. The NSLog statement I wrote in it never gets executed!

然后我尝试了这个——像这样在 attributeMapping 上写一个转换器:

So then I tried this -- writing a transformer on the attributeMapping like this:

RKValueTransformer* transformer = [self timeIntervalInMillisecondsSince1970TwoWayTransformer];
RKAttributeMapping *tokenExpiryMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"access_token_expiration" toKeyPath:@"accessTokenExpirationDate"];
tokenExpiryMapping.valueTransformer = transformer;
[userMapping addPropertyMapping:tokenExpiryMapping];

但是当我这样做时代码没有编译.它说在'RKAttributeMapping *'类型的对象上找不到Property valueTransformer".我不明白这个.我在互联网上看到的所有示例,包括带有 RestKit 文档 的示例同样的事情.所有示例都设置了 AttributeMapping 对象的 valueTransformer 属性.甚至 RestKit 类参考 也说我可以设置它.但是为什么不编译呢?

But the code doesn't compile when I do this. It says "Property valueTransformer not found on object of type 'RKAttributeMapping *'". I don't understand this. All the examples I have seen on the internet, including the one with RestKit's documentation say the same thing. All examples set the valueTransformer property of the AttributeMapping object. Even the RestKit Class Reference says that I can set it. But then why doesn't it compile?

推荐答案

所以我终于弄清楚了问题所在.

So I managed to finally figure out what the problem was.

我的 podfile 被配置为使用 RestKit 0.20.3.然后我添加了 RKValueTransformers 作为另一个 Pod.但是,RestKit 0.20.3 及更早版本带有自己的 RKValueTransformer(.h 和 .m)文件版本.而且那些旧版本的 RestKit 不支持添加您自己的 Transformers,因为它没有使用较新的 RKValueTransformers 库.

My podfile was configured to use RestKit 0.20.3. And then I had added RKValueTransformers as another Pod. However, RestKit 0.20.3 and earlier come with their own version of RKValueTransformer (.h and .m) files. And those older versions of RestKit doesn't support adding your own Transformers, because it doesn't make use of the newer RKValueTransformers library.

当我将我的 RestKit 版本升级到最新版本时(实际上任何高于 0.21.0 的版本都可以工作),一切开始正常.我不需要添加 pod RKValueTransformers,因为它是作为 RestKit 的依赖项自动添加的.

When I upgraded my RestKit version to the newest version (actually anything above 0.21.0 would work), things started working fine. I didn't need to add the pod RKValueTransformers, because it gets added automatically as a dependency of RestKit.

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

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