简体   繁体   English

在目标c中使用jsonModel映射时,将默认值设置为null字段

[英]Setting default values to null fields when mapping with jsonModel in objective c

I have a model which is the response of server. 我有一个模型,它是服务器的响应。 one of its properties is set to null from server. 它的属性之一从服务器设置为null。 How can I set it to empty string when receiving null? 收到null时如何将其设置为空字符串?

Model: 模型:

@interface infoResponseEntity : JSONModel

@property (retain, nullable) NSString *date;
@property (retain, nonatomic) NSString *time;
@property (retain, nullable) NSString *description;

@end

For more convenient you can write an extender for NSString. 为了更方便,您可以为NSString编写扩展程序。 This is .h file 这是.h文件

@interface NSString (Extender)

+ (BOOL)isNull:(NSString *)string;

@end

and this is .m file 这是.m文件

@implementation NSString (Extender)


+ (BOOL)isNull:(NSString *)string;
{
    return (string == nil);
}

@end

Finally, you can use the above method for check value of property in your model. 最后,可以使用上述方法检查模型中的属性值。 For example: 例如:

if ([NSString isNull:self.date]) {

  self.date = @""; 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM