后台返回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种解决方案。请各位大神帮我分析利弊。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM