处理NULL问题

2018-02-08  本文已影响8人  忆白i
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
上一篇下一篇

猜你喜欢

热点阅读