1。第一种解决方案 javascript
就是在每个 可能传回null 的地方 使用 if([object isEqual:[NSNUll null]]) 去判断html
2。第二种解决方案java
网上传说老外写了一个Category,叫作NullSafe..只支持到ios9,3 ,实测 并无解决个人问题..ios
NullSafe的原理见 http://www.javashuo.com/article/p-hsfhknmc-by.htmlgit
3。第三种解决方案github
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];json
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];数组
serializer.removesKeysWithNullValues = YES;网络
[serializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil]];app
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种解决方案。请各位大神帮我分析利弊。