后台返回null iOS

 

1。第一种解决方案  

就是在每一个 可能传回null 的地方 使用  if([object isEqual:[NSNUll null]]) 去判断

2。第二种解决方案

网上传说老外写了一个Category,叫做NullSafe..只支持到ios9,3  ,实测 并没有解决我的问题..

NullSafe的原理见 https://www.cnblogs.com/H7N9/p/5908149.html

3。第三种解决方案

 

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];

    serializer.removesKeysWithNullValues = YES;

    [serializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil]];

    manager.responseSerializer=serializer;

    manager.requestSerializer = [AFJSONRequestSerializer serializer];

不知道为什么无效,有大神给指导一下吗

4。第四种解决方案

json转model时,每个model里加上

 

- (id)valueForUndefinedKey:(NSString *)key

{

    return nil;

}

 

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    

}

 

5。第5种解决方案

在数组和字典的category里面写的两个方法. 完美解决..在每次请求回来数据,统一处理.. 

废话不多说,上代码 

字典的类目:

 

#import "NSDictionary+Extension.h"

#import "NSArray+Extension.h"

@implementation NSDictionary (Extension)

- (NSDictionary *)dictionaryByReplacingNulls {

    

    const NSMutableDictionary *replaced = [self mutableCopy];

    

    const id nul = [NSNull null];

    

    for (NSString *key in self) {

        

        id object = [self objectForKey:key];

        

        if (object == nul) [replaced removeObjectForKey:key];

        

        else if ([object isKindOfClass:[NSDictionary class]]) [replaced setObject:[object dictionaryByReplacingNulls] forKey:key];

        

        else if ([object isKindOfClass:[NSArray class]]) [replaced setObject:[object arrayByReplacingNulls] forKey:key];

        

    }

    

    return [NSDictionary dictionaryWithDictionary:[replaced copy]];

    

}

@end

 

 

 

数组的类目:

 

#import "NSArray+Extension.h"

#import "NSDictionary+Extension.h"

@implementation NSArray (Extension)

- (NSArray *)arrayByReplacingNulls  {

    

    NSMutableArray *replaced = [self mutableCopy];

    

    const id nul = [NSNull null];

    

    for (int idx = 0; idx < [replaced count]; idx++) {

        

        id object = [replaced objectAtIndex:idx];

        

        if (object == nul) [replaced removeObjectAtIndex:idx];

        

        else if ([object isKindOfClass:[NSDictionary class]]) [replaced replaceObjectAtIndex:idx withObject:[object dictionaryByReplacingNulls]];

        

        else if ([object isKindOfClass:[NSArray class]]) [replaced replaceObjectAtIndex:idx withObject:[object arrayByReplacingNulls]];

        

    }

    

    return [replaced copy];

    

}

 

 

@end

 

 

在封装的网络请求获取到后台数据的地方 将数据处理一下

 

NSDictionary *dict = [responseObject dictionaryByReplacingNulls];

success(dict);

 

6。第6种解决方案 

大力推荐:

AvoidCrash再也不怕程序崩溃啦 AvoidCrash的功能不仅仅是数组,还有报错字典等许多功能,具体用法可以参考https://github.com/chenfanfang/AvoidCrash

 

我的项目目前调用了3456这4种解决方案。请各位大神帮我分析利弊。

 

转载于:https://www.cnblogs.com/huangzs/p/7450885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值