如何删除NSDictionary或NSArray中的NSNull

前段时间与某公司的技术交流,被问到一个问题,如何删除NSDictionary中的NSNull。当时在纸上写,以前太依赖Xcode编译器了,以至于方法名都写不全,最终也没写出来,我想我肯定被鄙视的体无完肤。

最近有机会实现了一下。先来递归实现方案:

// 删除NSArray中的NSNull
- (NSMutableArray *)removeNullFromArray:(NSArray *)arr
{
NSMutableArray *marr = [NSMutableArray array];
for (int i = 0; i < arr.count; i++) {
NSValue *value = arr[i];
// 删除NSDictionary中的NSNull,再添加进数组
if ([value isKindOfClass:NSDictionary.class]) {
[marr addObject:[self removeNullFromDictionary:(NSDictionary *)value]];
}
// 删除NSArray中的NSNull,再添加进数组
else if ([value isKindOfClass:NSArray.class]) {
[marr addObject:[self removeNullFromArray:(NSArray *)value]];
}
// 剩余的非NSNull类型的数据添加进数组
else if (![value isKindOfClass:NSNull.class]) {
[marr addObject:value];
}
}
return marr;
}
// 删除Dictionary中的NSNull
- (NSMutableDictionary *)removeNullFromDictionary:(NSDictionary *)dic
{
NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
for (NSString *strKey in dic.allKeys) {
NSValue *value = dic[strKey];
// 删除NSDictionary中的NSNull,再保存进字典
if ([value isKindOfClass:NSDictionary.class]) {
mdic[strKey] = [self removeNullFromDictionary:(NSDictionary *)value];
}
// 删除NSArray中的NSNull,再保存进字典
else if ([value isKindOfClass:NSArray.class]) {
mdic[strKey] = [self removeNullFromArray:(NSArray *)value];
}
// 剩余的非NSNull类型的数据保存进字典
else if (![value isKindOfClass:NSNull.class]) {
mdic[strKey] = dic[strKey];
}
}
return mdic;
}

很美观哈。俩方法,第一个方法能将NSArray中的NSNull删除,第二方法能将NSDictionary中的NSNull删除。



        可是,可是,我更喜欢非递归算法的呀,尤其在AI搜索里。用递归的话,很担心考虑不周全导致少计算一大块。下面,就来一发非递归的方案。代码如下:

- (NSObject *)removeNullFrom:(NSObject *)object
{
NSObject *objResult = nil;
NSMutableArray *marrSearch = nil;
if ([object isKindOfClass:NSNull.class]) {
return nil;
}
else if ([object isKindOfClass:NSArray.class]) {
objResult = [NSMutableArray arrayWithArray:(NSArray *)object];
marrSearch = [NSMutableArray arrayWithObject:objResult];
}
else if ([object isKindOfClass:NSDictionary.class]) {
objResult = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)object];
marrSearch = [NSMutableArray arrayWithObject:objResult];
}
else {
return object;
}
while (marrSearch.count > 0) {
NSObject *header = marrSearch[0];
if ([header isKindOfClass:NSMutableDictionary.class]) {
// 遍历这个字典
NSMutableDictionary *mdicTemp = (NSMutableDictionary *)header;
for (NSString *strKey in mdicTemp.allKeys) {
NSObject *objTemp = mdicTemp[strKey];
// 将NSDictionary替换为NSMutableDictionary
if ([objTemp isKindOfClass:NSDictionary.class]) {
NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)objTemp];
mdicTemp[strKey] = mdic;
[marrSearch addObject:mdic];
}
// 将NSArray替换为NSMutableArray
else if ([objTemp isKindOfClass:NSArray.class]) {
NSMutableArray *marr = [NSMutableArray arrayWithArray:(NSArray *)objTemp];
mdicTemp[strKey] = marr;
[marrSearch addObject:marr];
}
// 删除NSNull
else if ([objTemp isKindOfClass:NSNull.class]) {
mdicTemp[strKey] = nil;
}
}
}
else if ([header isKindOfClass:NSMutableArray.class]) {
// 遍历这个数组
NSMutableArray *marrTemp = (NSMutableArray *)header;
for (int i = marrTemp.count-1; i >= 0; i--) {
NSObject *objTemp = marrTemp[i];
// 将NSDictionary替换为NSMutableDictionary
if ([objTemp isKindOfClass:NSDictionary.class]) {
NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)objTemp];
[marrTemp replaceObjectAtIndex:i withObject:mdic];
[marrSearch addObject:mdic];
}
// 将NSArray替换为NSMutableArray
else if ([objTemp isKindOfClass:NSArray.class]) {
NSMutableArray *marr = [NSMutableArray arrayWithArray:(NSArray *)objTemp];
[marrTemp replaceObjectAtIndex:i withObject:marr];
[marrSearch addObject:marr];
}
// 删除NSNull
else if ([objTemp isKindOfClass:NSNull.class]) {
[marrTemp removeObjectAtIndex:i];
}
}
}
else {
// 到这里就出错了
}
[marrSearch removeObjectAtIndex:0];
}
return objResult;
}

一个方法,所有类型全搞定。

发帖地址:http://www.cocoachina.com/bbs/read.php?tid=331661

上一篇:Java学习笔记39(转换流)


下一篇:Servlet、MySQL中文乱码