Search code examples
iosobjective-crestkit

RestKit: Values to BOOL


I have an NS_ENUM that holds the status of a checklist. The two properties are Pending and Completed.

typedef NS_ENUM (NSUInteger, ChecklistStatus) { Pending, Completed };

I am trying to take the status and map that to a true/false value. The method i'm doing this with is via RKValueTransformer.

Here is the code for that:

+(RKValueTransformer *)checklistStatusToBooleanTransformer
{
    RKValueTransformer *transformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class sourceClass, __unsafe_unretained Class destinationClass) {
        return ([sourceClass isSubclassOfClass:[NSNumber class]]);
    } transformationBlock:^BOOL(NSNumber *inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputClass, NSError *__autoreleasing *error) {
        // validate the input
        RKValueTransformerTestInputValueIsKindOfClass(inputValue, [NSNumber class], error);
        if([inputValue isEqual:@(Completed)]) {
            *outputValue = [NSNumber numberWithBool:YES];
        } else {
            *outputValue = [NSNumber numberWithBool:YES];
        }

        return YES;
    }];

    return transformer;
}

I'm not able to explicitly cast my output value as a BOOL, so i'm really lost as to what I should do here.

Any help would be greatly appreciated!


Solution

  • You can't explicitly convert, since BOOL isn't a primitive type. Check if the desired output is a string "false"/"true" instead of actualy values of t/f