Last active
December 11, 2017 09:00
-
-
Save gintsmurans/5031198 to your computer and use it in GitHub Desktop.
Recursively remove nulls from NSArray and NSDictionary
This file contains hidden or 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 NSMutableArray (Custom) | |
- (NSMutableArray *)replaceNullsWithObject:(id)object; | |
@end | |
@interface NSMutableDictionary (Custom) | |
- (NSMutableDictionary *)replaceNullsWithObject:(id)object; | |
@end |
This file contains hidden or 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+RemoveNulls.h" | |
@implementation NSArray (Custom) | |
- (NSMutableArray *)replaceNullsWithObject:(id)object | |
{ | |
NSMutableArray *tmp_array = [self mutableCopy]; | |
int count = tmp_array.count; | |
for (int i = 0; i < count; ++i) | |
{ | |
id tmp = [tmp_array objectAtIndex:i]; | |
if ([tmp isKindOfClass:[NSArray class]]) | |
{ | |
tmp = [tmp replaceNullsWithObject:object]; | |
[tmp_array replaceObjectAtIndex:i withObject:tmp]; | |
} | |
else if ([tmp isKindOfClass:[NSDictionary class]]) | |
{ | |
tmp = [tmp replaceNullsWithObject:object]; | |
[tmp_array replaceObjectAtIndex:i withObject:tmp]; | |
} | |
else if ([tmp isEqual:[NSNull null]]) | |
{ | |
[tmp_array replaceObjectAtIndex:i withObject:object]; | |
} | |
} | |
return tmp_array; | |
} | |
@end | |
@implementation NSDictionary (Custom) | |
- (NSMutableDictionary *)replaceNullsWithObject:(id)object | |
{ | |
NSMutableDictionary *tmp_dict = [self mutableCopy]; | |
for (NSString *key in [tmp_dict allKeys]) | |
{ | |
id tmp = [tmp_dict objectForKey:key]; | |
if ([tmp isKindOfClass:[NSArray class]]) | |
{ | |
tmp = [tmp replaceNullsWithObject:object]; | |
[tmp_dict setObject:tmp forKey:key]; | |
} | |
else if ([tmp isKindOfClass:[NSDictionary class]]) | |
{ | |
tmp = [tmp replaceNullsWithObject:object]; | |
[tmp_dict setObject:tmp forKey:key]; | |
} | |
else if ([tmp isEqual:[NSNull null]]) | |
{ | |
[tmp_dict setObject:object forKey:key]; | |
} | |
} | |
return tmp_dict; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment