Skip to content

Instantly share code, notes, and snippets.

@zhugexiaobo
Created May 28, 2014 08:49
Show Gist options
  • Save zhugexiaobo/27f85298ce0cb311fdea to your computer and use it in GitHub Desktop.
Save zhugexiaobo/27f85298ce0cb311fdea to your computer and use it in GitHub Desktop.
Replace all NSNull objects in an NSArray
@interface NSArray (NullReplacement)
- (NSArray *)arrayByReplacingNullsWithBlanks;
@end
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment