地幔会自动将0和1转换为BOOL吗? [英] Mantle convert 0 and 1 to BOOL automatically?

查看:141
本文介绍了地幔会自动将0和1转换为BOOL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mantle是否已将JSON中的int值0和1转换为Objective-C BOOL值?

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

我有一个模特:

@interface MyModel : MTLModel

@property (nonatomic, readonly) BOOL isValid;

@end

并且可以说JSON是:

And lets say JSON is:

{ is_valid: 0 } OR { is_valid: 1 }

我想知道Mantle是否会自动将is_valid转换为Objective-C BOOL值,我必须明确提及以下内容:

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.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆