替换NSDictionary中的所有NSNull对象

Seb*_*eek 13 objective-c nsdictionary json-framework nsstring nsnull

我很好奇,由于json-framework的帮助,我目前有NSDictionary一些值被设置为一个NSNull对象.

目的是剥离所有NSNull值并用空字符串替换它.

我确定有人在某处做过这件事吗?毫无疑问,这可能是一个四轮班轮,而且很简单,我只是太费力了,无法自己解决这个问题.

谢谢!

Sta*_*org 44

我对Jacob的原始答案进行了一些更改,以扩展它以处理存储在原始字典中的字典和数组.

#import "NSDictionary+NullReplacement.h"
#import "NSArray+NullReplacement.h"

@implementation NSDictionary (NullReplacement)

- (NSDictionary *)dictionaryByReplacingNullsWithBlanks {
    const NSMutableDictionary *replaced = [self mutableCopy];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for (NSString *key in self) {
        id object = [self objectForKey:key];
        if (object == nul) [replaced setObject:blank forKey:key];
        else if ([object isKindOfClass:[NSDictionary class]]) [replaced setObject:[object dictionaryByReplacingNullsWithBlanks] forKey:key];
        else if ([object isKindOfClass:[NSArray class]]) [replaced setObject:[object arrayByReplacingNullsWithBlanks] forKey:key];
    }
    return [NSDictionary dictionaryWithDictionary:[replaced copy]];
}

@end
Run Code Online (Sandbox Code Playgroud)

当然还有一个数组类别:

#import "NSArray+NullReplacement.h"
#import "NSDictionary+NullReplacement.h"

@implementation NSArray (NullReplacement)

- (NSArray *)arrayByReplacingNullsWithBlanks  {
    NSMutableArray *replaced = [self mutableCopy];
    const id nul = [NSNull null];
    const NSString *blank = @"";
    for (int idx = 0; idx < [replaced count]; idx++) {
        id object = [replaced objectAtIndex:idx];
        if (object == nul) [replaced replaceObjectAtIndex:idx withObject:blank];
        else if ([object isKindOfClass:[NSDictionary class]]) [replaced replaceObjectAtIndex:idx withObject:[object dictionaryByReplacingNullsWithBlanks]];
        else if ([object isKindOfClass:[NSArray class]]) [replaced replaceObjectAtIndex:idx withObject:[object arrayByReplacingNullsWithBlanks]];
    }
    return [replaced copy];
}

@end
Run Code Online (Sandbox Code Playgroud)

有了这个,您可以获取任何数组或字典,并递归清除所有[NSNull null]实例.

希望它可以帮助某人.

布兰登

PS为完成起见,这里是头文件:

@interface NSDictionary (NullReplacement)

- (NSDictionary *)dictionaryByReplacingNullsWithBlanks;

@end
Run Code Online (Sandbox Code Playgroud)

和数组头:

@interface NSArray (NullReplacement)

- (NSArray *)arrayByReplacingNullsWithBlanks;

@end
Run Code Online (Sandbox Code Playgroud)

  • 优秀的东西!非常感谢您使用此代码.我终于可以使用"NSPropertyListSerialization"将我的JSON结果缓存到本地文件,而不用担心NSDictionary中的CFNulls.Merci beaucoup unddankeschön. (2认同)

Jac*_*kin 16

真的很简单:

@interface NSDictionary (JRAdditions)
- (NSDictionary *)dictionaryByReplacingNullsWithStrings;
@end

@implementation NSDictionary (JRAdditions)

- (NSDictionary *)dictionaryByReplacingNullsWithStrings {
   const NSMutableDictionary *replaced = [self mutableCopy];
   const id nul = [NSNull null];
   const NSString *blank = @"";

   for(NSString *key in self) {
      const id object = [self objectForKey:key];
      if(object == nul) {
         //pointer comparison is way faster than -isKindOfClass:
         //since [NSNull null] is a singleton, they'll all point to the same
         //location in memory.
         [replaced setObject:blank 
                      forKey:key];
      }
   }

   return [replaced copy];
}

@end
Run Code Online (Sandbox Code Playgroud)

用法:

NSDictionary *someDictThatHasNulls = ...;
NSDictionary *replacedDict = [someDictThatHasNulls dictionaryByReplacingNullsWithStrings];
Run Code Online (Sandbox Code Playgroud)

  • 或者,因为`NSNull`是一个单例,你可以直接测试值指针:`if([self objectForKey:key] == [NSNull null])`. (7认同)
  • *是*..**但是**如果字典包含NULL的NSArray**怎么办?或其他**NSDictionary**与NULL的值?小心使用这个!我强烈建议使用Stakenborg对此方法的修改,如下所述! (3认同)
  • 使用Stakenborg的方法! (2认同)

Cam*_*mer 6

滚动字典搜索NSNull是解决问题的一种方法,但我采取了一种稍微懒散的方法.你可以分配一个空字符串而不是nil,但原理是相同的.

CPJSONDictionary.h

@interface NSDictionary (CPJSONDictionary)
- (id)jsonObjectForKey:(id)aKey;
@end
Run Code Online (Sandbox Code Playgroud)

CPJSONDictionary.m

@implementation NSDictionary (CPJSONDictionary)

- (id)jsonObjectForKey:(id)aKey {
    id object = [self objectForKey:aKey];
    if ([object isKindOfClass:[NSNull class]]) {
        object = nil;
    }

    return object;
}

@end
Run Code Online (Sandbox Code Playgroud)