nefarianblack nefarianblack - 10 months ago 36
Objective-C Question

Mantle convert 0 and 1 to BOOL automatically?

Does Mantle already converts int values 0 and 1 in JSON to objective-C BOOL values?

I have a model:

@interface MyModel : MTLModel

@property (nonatomic, readonly) BOOL isValid;


And lets say JSON is:

{ is_valid: 0 } OR { is_valid: 1 }

I want to know if Mantle would automatically convert
into Objective-C BOOL value to do I have to explicity mention the following:

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


Yes, this is handled for you; you don't need to use the mapping transformer.

Mantle internally calls setValue:forKey: to set the value. The 0 or 1 will be an NSNumber, and setValue:forKey: will use the boolValue on NSNumber to get a value, as your property is declared as a BOOL.

This behaviour of Key-Value Coding is described under Scalar and Structure Support.