Created
August 15, 2012 19:00
-
-
Save conradev/3362607 to your computer and use it in GitHub Desktop.
Recursively remove NSNull instances
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
// Use case being removing null values from the results of JSON encoding (thus only supports dictionaries and arrays) | |
__block id (^removeNull)(id) = ^(id rootObject) { | |
// Recurse through dictionaries | |
if ([rootObject isKindOfClass:[NSDictionary class]]) { | |
NSMutableDictionary *sanitizedDictionary = [NSMutableDictionary dictionaryWithDictionary:rootObject]; | |
[rootObject enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | |
id sanitized = removeNull(obj); | |
if (!sanitized) { | |
[sanitizedDictionary removeObjectForKey:key]; | |
} else { | |
[sanitizedDictionary setObject:sanitized forKey:key]; | |
} | |
}]; | |
return [NSDictionary dictionaryWithDictionary:sanitizedDictionary]; | |
} | |
// Recurse through arrays | |
if ([rootObject isKindOfClass:[NSArray class]]) { | |
NSMutableArray *sanitizedArray = [NSMutableArray arrayWithArray:rootObject]; | |
[rootObject enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
id sanitized = removeNull(obj); | |
if (!sanitized) { | |
[sanitizedArray removeObjectIdenticalTo:obj]; | |
} else { | |
[sanitizedArray replaceObjectAtIndex:[sanitizedArray indexOfObject:obj] withObject:sanitized]; | |
} | |
}]; | |
return [NSArray arrayWithArray:sanitizedArray]; | |
} | |
// Base case | |
if ([rootObject isKindOfClass:[NSNull class]]) { | |
return (id)nil; | |
} else { | |
return rootObject; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Xcode warning on line 7: