Created
May 28, 2014 05:30
-
-
Save dnnta/4722c81f141167f75a43 to your computer and use it in GitHub Desktop.
NSDictionary或者NSArray中Null处理
Replace all NSNull objects in an NSDictionary
http://stackoverflow.com/questions/8075147/replace-all-nsnull-objects-in-an-nsdictionary#16702060
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
#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 |
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
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment