MJExtension与YYModel对比
1, 老版本mjextension 类型转化有问题。如果json传过来的类型与模型定义类型不匹配,会导致从模型读出来的对象实际类型不匹配,进而在使用model对象的属性值时,引发方法找不到崩溃
比如:模型是字典类型,用mj_objectArrayWithKeyValuesArray把一个json数组转化为model数组,这个字典类型在json里面是字符串,模型中的就变成了字符串,而不是需要的nil类型
2,新版本类型转化 3.4.1以上版本进行测试
如果model定义为基础数值数据类型,比如bool,int,float,cgfloat等,json里面的对应key的value值是非字符串、非数值,非null等其他对象类型,数组或者字典,则会引发赋值崩溃。

kvc赋值
-(void)testkvc{
TypeTransferModel*a=[TypeTransferModel new];
[a setValue:@"1" forKey:@"boola"];
[a setValue:@1 forKey:@"boola"];
[a setValue:@1.23 forKey:@"boola"];
[a setValue:@true forKey:@"boola"];
[a setValue:@"asdfads" forKey:@"boola"];
//Skips initial space characters (whitespaceSet), or optional -/+ sign followed by zeroes. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. It ignores any trailing characters.
//去掉加减号和空白,然后首字母是1-9,tTYy都是true,否则是false
[@"1" boolValue];
//[a setValue:@{@"1":@"2"} forKey:@"boola"];//crash
//[a setValue:@[@"123"] forKey:@"boola"];//crash
//[a setValue:[NSNull null] forKey:@"boola"];//crash
}
3, 使用yymodel
则不会有问题
测试json串
[
{
"boola":true,
"str":"123",
"number":123,
"inta":123,
"floata":123.16,
"cgfloata":123.17,
"dicta":{
"key":"value"
},
"arraya":[
"a",
"12"
]
},
{
"boola":"123",
"str":"123",
"number":123,
"inta":123,
"floata":123.16,
"cgfloata":123.17,
"dicta":true,
"arraya":true
},
{
"boola":1,
"str":"123",
"number":123,
"inta":123,
"floata":123.16,
"cgfloata":123.17,
"dicta":"12",
"arraya":"12"
},
{
"boola":null,
"str":"123",
"number":123,
"inta":123,
"floata":123.16,
"cgfloata":123.17,
"dicta":[
"a",
"12"
],
"arraya":{
"key":"value"
}
},
{
"boola":null,
"str":123,
"number":"123",
"inta":"123",
"floata":"123.1",
"cgfloata":"123.17",
"dicta":1,
"arraya":1
},
{
"boola":null,
"str":null,
"number":null,
"inta":null,
"floata":null,
"cgfloata":null,
"dicta":null,
"arraya":null
},
{
"boola":[
"123"
],
"str":[
"123"
],
"number":[
"123"
],
"inta":[
"123"
],
"floata":[
"123"
],
"cgfloata":[
"123"
],
"dicta":[
"123"
],
"arraya":[
"123"
]
}
]
测试代码
@interface TypeTransferModel : NSObject
@property (nonatomic, assign) BOOL boola;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSNumber *number;
@property (nonatomic, assign) NSInteger inta;
@property (nonatomic, assign) float floata; //如果json传的是字符串,精读可能会有问题:"123.1"-》 123.099998
@property (nonatomic, assign) CGFloat cgfloata;
@property (nonatomic, strong) NSDictionary *dicta;
@property (nonatomic, strong) NSArray *arraya;
@end
-(void)test4{
NSString*jsonstr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"json.txt" ofType:nil] encoding:NSUTF8StringEncoding error:nil];
NSArray *r = [TypeTransferModel mj_objectArrayWithKeyValuesArray:jsonstr];
NSLog(@"%@",r);
[r enumerateObjectsUsingBlock:^(NSObject* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj.mj_keyValues);
}];
}
-(void)test5{
NSString*jsonstr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"jsonyy.txt" ofType:nil] encoding:NSUTF8StringEncoding error:nil];
NSArray *r= [NSArray modelArrayWithClass:[TypeTransferModel class] json:jsonstr];
NSLog(@"%@",r);
NSLog(@"%@", [r modelToJSONString]);
}
网友评论