Question

I am trying to create a custom RKValueTransformer for making a transformation to the data received. An API that I am using returns the latitude without any decimal point. I would like to be able to make some processing and add the decimal point myself. I am able to map the 'lat' field into the actual class:

Found transformable value at keyPath 'lat'. Transforming from class '__NSCFNumber' to 'NSNumber'RKValueTransformer

I created a RKValueTransformer, but it is never called when the data is being mapped.

- (void)setupValueTransformers {

    [[RKValueTransformer defaultValueTransformer] addValueTransformer:[self coordinatesValueTransformer]];

    [[RKValueTransformer defaultValueTransformer] insertValueTransformer:[self coordinatesValueTransformer] atIndex:0];
}

- (RKValueTransformer *)coordinatesValueTransformer {
    return [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class inputValueClass, __unsafe_unretained Class outputValueClass) {

        return ([inputValueClass isSubclassOfClass:[NSNumber class]] && [outputValueClass isSubclassOfClass:[NSNumber class]]);
    } transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputClass, NSError *__autoreleasing *error) {

      //MY PROCESSING here

        return YES;
    }];
}
Was it helpful?

Solution

If the source class matches the destination class then no conversion is required and your transformer will not be called.

KVC validation is the appropriate place for this logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top