iOS实战之后台返回数据为空造成程序崩溃 - 简书

字数 34阅读 687
方法一:使用AFNetworking进行请求过滤设置:
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;

方法二:使用runtime来给类别增加属性

NSNull+Safe.m

#import <objc/runtime.h>
#import <Foundation/Foundation.h>
static NSString *dataKey = @"key";

@interface NSNull ()

@property (strong, nonatomic) NSMutableDictionary *data;

@end

@implementation NSNull (Safe)

#pragma mark - LifeCycle

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
    @synchronized([self class])
    {
        NSString *selString = NSStringFromSelector(selector);
        NSMethodSignature *signature = nil;
        
        NSRange range = [selString rangeOfString:@"set"];
        
        if (range.length) {
            signature = [NSMethodSignature signatureWithObjCTypes:"v@:@"];
        } else {
            signature = [NSMethodSignature signatureWithObjCTypes:"@@:"];
        }
        
        return signature;
    }
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
    NSString *key = NSStringFromSelector(invocation.selector);

    NSRange range = [key rangeOfString:@"set"];

    if (range.length) {
        key = [[key substringFromIndex:3] lowercaseString];
        id obj;
        [invocation getArgument:&obj atIndex:2];
        [self.data setObject:obj forKey:key];
    } else {
        id obj = self.data[key];
        [invocation setReturnValue:&obj];
    }

}

#pragma mark - Private

- (NSMutableDictionary *)data {
    NSMutableDictionary *data = objc_getAssociatedObject(self, &dataKey);

    while (!data) {
        objc_setAssociatedObject(self, &dataKey, [NSMutableDictionary dictionary], OBJC_ASSOCIATION_COPY_NONATOMIC);
        data = objc_getAssociatedObject(self, &dataKey);
    }

    return data;
}


@end

推荐阅读更多精彩内容