how to process BOOL in Mantle?

589 Views Asked by At

I have MyModel inheriting from MTLModel (using the GitHub Mantle pod). MyModel.h

@interface MyModel : MTLModel <MTLJSONSerializing>

@property (nonatomic, assign, readonly) BOOL *nsfw;

@end

in MyModel.m, I have tried

+ (NSValueTransformer *)nsfwJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLBooleanValueTransformerName];
}

and

+ (NSValueTransformer *)nsfwJSONTransformer {
    return [NSValueTransformer mtl_valueMappingTransformerWithDictionary:@{
                                                                           @"0": @(NO),
                                                                           @"1": @(YES)
                                                                               }];
}

both not working

and using Charles get the field in Response JSON data, screenshots as shown .

meanwhile, in the dictionary serialized, the relevant fields is

nsfw = 0

futher, the results of breakpoint debug in here

It's not my expect, I think it should be nsfw = false something like that

I feel something is wrong..

So does this mean that Boolean becomes NSNumber by serialization?

What should I do to solve the problem?

any tips or suggestion is appreciate~

1

There are 1 best solutions below

0
Michael Dautermann On BEST ANSWER

As you probably know by looking at this related question, Mantle already converts int values 0 and 1 in JSON to Objective-C BOOL values.

I think your problem comes down to a simple typo.

Instead of:

@property (nonatomic, assign, readonly) BOOL *nsfw;

which is a pointer.

Do:

@property (nonatomic, assign, readonly) BOOL nsfw;

which is the C type that Mantle knows to translate into.