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;
@end
And lets say JSON is:
{ is_valid: 0 } OR { is_valid: 1 }
I want to know if Mantle would automatically convert is_valid
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.
If you need to convert a string to boolean use this transformer:
+ (NSValueTransformer *)sectionJSONTransformer {
return [NSValueTransformer mtl_valueMappingTransformerWithDictionary:@{@"0" : @(NO),
@"1" : @(YES)}];
}
+ (NSValueTransformer *)awesomeJSONTransformer {
return [NSValueTransformer valueTransformerForName: MTLBooleanValueTransformerName];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With