处理NULL问题

image.png

有时候后台在偷懒的的时候 给你返回NULL 那么问题就来了。。。

MySelfWorkInfoModel.h

#import <Foundation/Foundation.h>
@interface MySelfWorkInfoModel : NSObject
@property (nonatomic ,assign) NSUInteger userInfoId;
@property (nonatomic ,strong) NSString   *headPicAdd;
@property (nonatomic ,strong) NSString   *nick;
@property (nonatomic ,assign) NSInteger  todayWorkTime;
@property (nonatomic ,strong) NSString   *workLong;
@property (nonatomic ,assign) NSInteger  successOrderNum;+ (instancetype)creatRankingTotalModelWith:(NSDictionary *)dict;
@end

MySelfWorkInfoModel.m

#import "MySelfWorkInfoModel.h"

@implementation MySelfWorkInfoModel
- (instancetype)initWithDict:(NSDictionary *)dict {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)creatRankingTotalModelWith:(NSDictionary *)dict {
    MySelfWorkInfoModel *model = [[self alloc] initWithDict:dict];
    return model;
}

- (void)setValue:(id)value forKey:(NSString *)key {
    value = [self setNoNull:value];
    [super setValue:value forKey:key];
}
//只需要下面这段代码搞定你所遇到的问题
- (id) setNoNull:(id)aValue{
    if (aValue == nil) {
        aValue = @"";//为null时,直接赋空
    } else if ((NSNull *)aValue == [NSNull null]) {
        aValue = @"";
        if ([aValue isEqual:nil]) {
            aValue = @"";
        }
    }
    return aValue;
}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,911评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,383评论 19 139
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,204评论 0 38
  • 如果说三角形是最稳固的形状,那么三个人的友情就是最摇摇欲坠的一种关系了。苏苏最近很不开心,因为升上初中以后她和小学...
    瑶瑶yoyo_阅读 256评论 1 4
  • 祝大家中秋快乐!阖家幸福!
    狄晓先阅读 167评论 0 1

友情链接更多精彩内容