美文网首页
处理NULL问题

处理NULL问题

作者: 忆白i | 来源:发表于2018-02-08 02:48 被阅读8次
    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
    

    相关文章

      网友评论

          本文标题:处理NULL问题

          本文链接:https://www.haomeiwen.com/subject/kuyzzxtx.html