Skip to content

Instantly share code, notes, and snippets.

@chrishulbert
Created November 30, 2011 02:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrishulbert/1407760 to your computer and use it in GitHub Desktop.
Save chrishulbert/1407760 to your computer and use it in GitHub Desktop.
Dictionary Types Helper
// DictionaryTypesHelper.h
#import <Foundation/Foundation.h>
@interface NSDictionary (NSDictionary_DictionaryTypesHelper)
- (NSArray*)arrayForKey:(id)key;
- (NSDictionary*)dictionaryForKey:(id)key;
- (NSString*)stringForKey:(id)key;
- (NSNumber*)numberForKey:(id)key;
- (NSData*)dataForKey:(id)key;
- (BOOL)boolForKey:(id)key;
- (float)floatForKey:(id)key;
- (int)intForKey:(id)key;
- (double)doubleForKey:(id)key;
@end
@interface NSObject (NSObject_WhatIsHelper)
- (BOOL)isDict;
- (BOOL)isArray;
- (BOOL)isString;
- (BOOL)isNull;
- (BOOL)isNumber;
- (BOOL)isData;
@end
// ---
// DictionaryTypesHelper.m
#import "DictionaryTypesHelper.h"
// Say we have a dictionary full of these types
// NSDictionary
// NSArray
// NSString
// NSNumber
// NSNull
// We need to ensure they are the correct types when pulling them out
// These functions return the values if correct, coax them correctly
// if possible, or return nil (not nsnull) if impossible
@implementation NSDictionary (NSDictionary_DictionaryTypesHelper)
// These ones don't attempt to coax - you're either an X or you're nil.
- (NSArray*)arrayForKey:(id)key {
id o = [self objectForKey:key];
return [o isArray] ? o : nil;
}
- (NSDictionary*)dictionaryForKey:(id)key {
id o = [self objectForKey:key];
return [o isDict] ? o : nil;
}
- (NSData*)dataForKey:(id)key {
id o = [self objectForKey:key];
return [o isData] ? o : nil;
}
// These ones try to coax, but return nil if they are not in the dictionary or are nsnulls
- (NSString*)stringForKey:(id)key {
id o = [self objectForKey:key];
if ([o isString]) return o;
if (!o || [o isNull]) return nil;
return [o description];
}
- (NSNumber*)numberForKey:(id)key {
id o = [self objectForKey:key];
if ([o isNumber]) return o;
if (!o || [o isNull]) return nil;
return [NSNumber numberWithLongLong:[o description].longLongValue];
}
// These ones simply return zero if they can't figure it out
- (BOOL)boolForKey:(id)key {
id o = [self objectForKey:key];
if ([o isNumber]) return [o boolValue];
return [o description].boolValue;
}
- (float)floatForKey:(id)key {
id o = [self objectForKey:key];
if ([o isNumber]) return [o floatValue];
return [o description].floatValue;
}
- (int)intForKey:(id)key {
id o = [self objectForKey:key];
if ([o isNumber]) return [o intValue];
return [o description].intValue;
}
- (double)doubleForKey:(id)key {
id o = [self objectForKey:key];
if ([o isNumber]) return [o doubleValue];
return [o description].doubleValue;
}
@end
@implementation NSObject (NSObject_WhatIsHelper)
- (BOOL)isDict {
return [self isKindOfClass:[NSDictionary class]];
}
- (BOOL)isArray {
return [self isKindOfClass:[NSArray class]];
}
- (BOOL)isString {
return [self isKindOfClass:[NSString class]];
}
- (BOOL)isNumber {
return [self isKindOfClass:[NSNumber class]];
}
- (BOOL)isNull {
return self==[NSNull null];
}
- (BOOL)isData {
return [self isKindOfClass:[NSData class]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment