ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

yymodel 某个属性当iOS以int接受 而接口返回null,json解析会崩溃不

yymodel 某个属性当iOS以int接受 而接口返回null,json解析会崩溃不
使用 YYModel 时,如果接口返回 null而 iOS 端用 int类型接收,​​不会崩溃,但会有默认值​​。

YYModel 的处理机制

1. 基本数据类型(int/NSInteger等)

@interface User : NSObject
@property (nonatomic, assign) int userId;
@property (nonatomic, assign) NSInteger age;
@end// JSON: {"userId": null, "age": null}
User *user = [User yy_modelWithJSON:json];
NSLog(@"%d", user.userId); // 输出: 0
NSLog(@"%ld", user.age);   // 输出: 0
​结果​​:不会崩溃,int类型会默认设置为 0

2. 对象类型(NSNumber *)

@interface User : NSObject
@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSNumber *age;
@end// JSON: {"userId": null, "age": 25}
User *user = [User yy_modelWithJSON:json];
NSLog(@"%@", user.userId); // 输出: (null)
NSLog(@"%@", user.age);    // 输出: 25
​结果​​:不会崩溃,对象类型会设置为 nil

安全的使用方式

方式一:使用 NSNumber(推荐)

@interface User : NSObject
@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSNumber *age;// 提供便捷访问方法
- (int)userIdIntValue;
- (int)ageIntValue;
@end@implementation User
- (int)userIdIntValue {return [self.userId intValue]; // 如果为nil返回0
}- (int)ageIntValue {return [self.age intValue]; // 如果为nil返回0
}
@end

方式二:自定义转换方法

@interface User : NSObject
@property (nonatomic, assign) int userId;
@property (nonatomic, assign) int age;// 自定义YYModel转换
+ (NSDictionary *)modelCustomPropertyMapper {return @{@"userId": @"id",@"age": @"age"};
}// 处理null值
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {// 手动处理可能的null值if (dic[@"id"] == [NSNull null]) {_userId = -1; // 设置特殊默认值}if (dic[@"age"] == [NSNull null]) {_age = -1; // 设置特殊默认值}return YES;
}
@end

方式三:使用类别安全解析

@interface NSDictionary (SafeYYModel)
- (id)safeObjectForKey:(id)key;
@end@implementation NSDictionary (SafeYYModel)
- (id)safeObjectForKey:(id)key {id obj = [self objectForKey:key];if (obj == [NSNull null]) {return nil;}return obj;
}
@end// 在model中自定义转换
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {NSNumber *userId = [dic safeObjectForKey:@"id"];_userId = userId ? [userId intValue] : 0;return YES;
}

实际使用建议

推荐方案:使用 NSNumber + 便捷方法

@interface User : NSObject
@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSNumber *age;
@property (nonatomic, strong) NSNumber *height;// 便捷访问方法
- (int32_t)userIdValue;
- (int32_t)ageValue;
- (float)heightValue;// 安全访问方法(带默认值)
- (int32_t)userIdValueWithDefault:(int32_t)defaultValue;
@end@implementation User
- (int32_t)userIdValue {return [self.userId intValue];
}- (int32_t)ageValue {return [self.age intValue];
}- (float)heightValue {return [self.height floatValue];
}- (int32_t)userIdValueWithDefault:(int32_t)defaultValue {return self.userId ? [self.userId intValue] : defaultValue;
}
@end

验证测试

// 测试数据
NSDictionary *json1 = @{@"id": @123, @"age": @25};        // 正常数据
NSDictionary *json2 = @{@"id": [NSNull null], @"age": @25}; // 部分null
NSDictionary *json3 = @{@"id": [NSNull null], @"age": [NSNull null]}; // 全部nullUser *user1 = [User yy_modelWithJSON:json1];
User *user2 = [User yy_modelWithJSON:json2]; 
User *user3 = [User yy_modelWithJSON:json3];// 都不会崩溃
NSLog(@"User1: %d", user1.userId); // 123
NSLog(@"User2: %d", user2.userId); // 0  
NSLog(@"User3: %d", user3.userId); // 0

总结

  • ​不会崩溃​​:YYModel 对 null值有安全处理
  • ​基本类型​​:null会被转换为 0
  • ​对象类型​​:null会被转换为 nil
  • ​推荐使用 NSNumber​​:更安全,能区分"无值"和"0值"
  • ​YYModel 在这方面比某些其他JSON库更安全​
返回列表