Created
May 28, 2014 08:49
-
-
Save zhugexiaobo/27f85298ce0cb311fdea to your computer and use it in GitHub Desktop.
Replace all NSNull objects in an NSArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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