Last active
December 15, 2015 15:59
-
-
Save jeremytregunna/5285974 to your computer and use it in GitHub Desktop.
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 <Foundation/Foundation.h> | |
@interface NSArray (SafeAccess) | |
- (instancetype)withoutNulls; | |
- (instancetype)arrayByRemovingObjectsOfClass:(id)obj; | |
- (id)objectAtIndex:(NSUInteger)index ifKindOf:(Class)kind; | |
- (id)objectAtIndex:(NSUInteger)index ifKindOf:(Class)kind defaultValue:(id)defaultValue; | |
- (NSArray*)arrayAtIndex:(NSUInteger)index; | |
- (NSArray*)arrayAtIndex:(NSUInteger)index defaultValue:(NSArray*)defaultValue; | |
- (NSDictionary*)dictionaryAtIndex:(NSUInteger)index; | |
- (NSDictionary*)dictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary*)defaultValue; | |
- (NSString*)stringAtIndex:(NSUInteger)index; | |
- (NSString*)stringAtIndex:(NSUInteger)index defaultValue:(NSString*)defaultValue; | |
- (NSNumber*)numberAtIndex:(NSUInteger)index; | |
- (NSNumber*)numberAtIndex:(NSUInteger)index defaultValue:(NSNumber*)defaultValue; | |
// -boolAtIndex: is not provided. You are the owner of your default value mistakes. | |
- (BOOL)boolAtIndex:(NSUInteger)index defaultValue:(BOOL)defaultValue; | |
- (NSDate*)dateAtIndex:(NSUInteger)index; | |
- (NSDate*)dateAtIndex:(NSUInteger)index defaultValue:(NSDate*)defaultValue; | |
@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+SafeAccess.h" | |
@implementation NSArray (SafeAccess) | |
- (instancetype)withoutNulls | |
{ | |
NSArray* temp = [self arrayByRemovingObjectsOfClass:[NSNull class]]; | |
NSMutableArray* result = [NSMutableArray arrayWithCapacity:[temp count]]; | |
for(id obj in temp) | |
{ | |
if([obj respondsToSelector:@selector(withoutNulls)]) | |
[result addObject:[obj withoutNulls]]; | |
else | |
[result addObject:obj]; | |
} | |
return [result copy]; | |
} | |
- (instancetype)arrayByRemovingObjectsOfClass:(Class)aClass | |
{ | |
NSMutableArray* results = [NSMutableArray array]; | |
for(id o in self) | |
{ | |
if([o isKindOfClass:aClass] == NO) | |
[results addObject:o]; | |
} | |
return [results copy]; | |
} | |
- (id)objectAtIndex:(NSUInteger)index ifKindOf:(Class)kind | |
{ | |
return [self objectAtIndex:index ifKindOf:kind defaultValue:nil]; | |
} | |
- (id)objectAtIndex:(NSUInteger)index ifKindOf:(Class)kind defaultValue:(id)defaultValue | |
{ | |
id obj = self[index]; | |
return [obj isKindOfClass:kind] ? obj : defaultValue; | |
} | |
#pragma mark - Type specific helpers | |
- (NSArray*)arrayAtIndex:(NSUInteger)index | |
{ | |
return [self arrayAtIndex:index defaultValue:@[]]; | |
} | |
- (NSArray*)arrayAtIndex:(NSUInteger)index defaultValue:(NSArray*)defaultValue | |
{ | |
return [self objectAtIndex:index ifKindOf:[NSArray class] defaultValue:defaultValue]; | |
} | |
- (NSDictionary*)dictionaryAtIndex:(NSUInteger)index | |
{ | |
return [self dictionaryAtIndex:index defaultValue:@{}]; | |
} | |
- (NSDictionary*)dictionaryAtIndex:(NSUInteger)index defaultValue:(NSDictionary*)defaultValue | |
{ | |
return [self objectAtIndex:index ifKindOf:[NSDictionary class] defaultValue:defaultValue]; | |
} | |
- (NSString*)stringAtIndex:(NSUInteger)index | |
{ | |
return [self stringAtIndex:index defaultValue:@""]; | |
} | |
- (NSString*)stringAtIndex:(NSUInteger)index defaultValue:(NSString*)defaultValue | |
{ | |
return [self objectAtIndex:index ifKindOf:[NSString class] defaultValue:defaultValue]; | |
} | |
- (NSNumber*)numberAtIndex:(NSUInteger)index | |
{ | |
return [self numberAtIndex:index defaultValue:nil]; | |
} | |
- (NSNumber*)numberAtIndex:(NSUInteger)index defaultValue:(NSNumber*)defaultValue | |
{ | |
return [self objectAtIndex:index ifKindOf:[NSNumber class] defaultValue:defaultValue]; | |
} | |
- (BOOL)boolAtIndex:(NSUInteger)index defaultValue:(BOOL)defaultValue | |
{ | |
NSNumber* defVal = @(defaultValue); | |
return [[self objectAtIndex:index ifKindOf:[NSNumber class] defaultValue:defVal] boolValue]; | |
} | |
- (NSDate*)dateAtIndex:(NSUInteger)index | |
{ | |
return [self dateAtIndex:index defaultValue:nil]; | |
} | |
- (NSDate*)dateAtIndex:(NSUInteger)index defaultValue:(NSDate*)defaultValue | |
{ | |
return [self objectAtIndex:index ifKindOf:[NSDate class] defaultValue:defaultValue]; | |
} | |
@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 <Foundation/Foundation.h> | |
@interface NSDictionary (SafeAccess) | |
- (instancetype)withoutNulls; | |
- (id)objectForKey:(id)key ifKindOf:(Class)kind; | |
- (id)objectForKey:(id)key ifKindOf:(Class)kind defaultValue:(id)defaultValue; | |
- (NSArray*)arrayForKey:(id)key; | |
- (NSArray*)arrayForKey:(id)key defaultValue:(NSArray*)defaultValue; | |
- (NSDictionary*)dictionaryForKey:(id)key; | |
- (NSDictionary*)dictionaryForKey:(id)key defaultValue:(NSDictionary*)defaultValue; | |
- (NSString*)stringForKey:(id)key; | |
- (NSString*)stringForKey:(id)key defaultValue:(NSString*)defaultValue; | |
- (NSNumber*)numberForKey:(id)key; | |
- (NSNumber*)numberForKey:(id)key defaultValue:(NSNumber*)defaultValue; | |
// -boolForKey: is not provided. You are the owner of your default value mistakes. | |
- (BOOL)boolForKey:(id)key defaultValue:(BOOL)defaultValue; | |
- (NSDate*)dateForKey:(id)key; | |
- (NSDate*)dateForKey:(id)key defaultValue:(NSDate*)defaultValue; | |
@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 "NSDictionary+SafeAccess.h" | |
@implementation NSDictionary (SafeAccess) | |
- (instancetype)withoutNulls | |
{ | |
NSMutableDictionary* replaced = [NSMutableDictionary dictionaryWithDictionary:self]; | |
NSString* blank = @""; | |
for(NSString* key in self) | |
{ | |
id object = [self objectForKey:key]; | |
if([object isKindOfClass:[NSNull class]] == YES) | |
[replaced setObject:blank forKey:key]; | |
else if([object isKindOfClass:[NSDictionary class]] == YES) | |
[replaced setObject:[(NSDictionary*)object withoutNulls] forKey:key]; | |
} | |
return [NSDictionary dictionaryWithDictionary: replaced]; | |
} | |
- (id)objectForKey:(id)key ifKindOf:(Class)kind | |
{ | |
return [self objectForKey:key ifKindOf:kind defaultValue:nil]; | |
} | |
- (id)objectForKey:(id)key ifKindOf:(Class)kind defaultValue:(id)defaultValue | |
{ | |
id obj = self[key]; | |
return [obj isKindOfClass:kind] ? obj : defaultValue; | |
} | |
#pragma mark - Type specific helpers | |
- (NSArray*)arrayForKey:(id)key | |
{ | |
return [self arrayForKey:key defaultValue:@[]]; | |
} | |
- (NSArray*)arrayForKey:(id)key defaultValue:(NSArray*)defaultValue | |
{ | |
return [self objectForKey:key ifKindOf:[NSArray class] defaultValue:defaultValue]; | |
} | |
- (NSDictionary*)dictionaryForKey:(id)key | |
{ | |
return [self dictionaryForKey:key defaultValue:@{}]; | |
} | |
- (NSDictionary*)dictionaryForKey:(id)key defaultValue:(NSDictionary*)defaultValue | |
{ | |
return [self objectForKey:key ifKindOf:[NSDictionary class] defaultValue:defaultValue]; | |
} | |
- (NSString*)stringForKey:(id)key | |
{ | |
return [self stringForKey:key defaultValue:@""]; | |
} | |
- (NSString*)stringForKey:(id)key defaultValue:(NSString*)defaultValue | |
{ | |
return [self objectForKey:key ifKindOf:[NSString class] defaultValue:defaultValue]; | |
} | |
- (NSNumber*)numberForKey:(id)key | |
{ | |
return [self numberForKey:key defaultValue:nil]; | |
} | |
- (NSNumber*)numberForKey:(id)key defaultValue:(NSNumber*)defaultValue | |
{ | |
return [self objectForKey:key ifKindOf:[NSNumber class] defaultValue:defaultValue]; | |
} | |
- (BOOL)boolForKey:(id)key defaultValue:(BOOL)defaultValue | |
{ | |
NSNumber* defVal = @(defaultValue); | |
return [[self objectForKey:key ifKindOf:[NSNumber class] defaultValue:defVal] boolValue]; | |
} | |
- (NSDate*)dateForKey:(id)key | |
{ | |
return [self dateForKey:key defaultValue:nil]; | |
} | |
- (NSDate*)dateForKey:(id)key defaultValue:(NSDate*)defaultValue | |
{ | |
return [self objectForKey:key ifKindOf:[NSDate class] defaultValue:defaultValue]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment