Skip to content

Instantly share code, notes, and snippets.

@khacanh
Created May 28, 2013 07:43
Show Gist options
  • Save khacanh/5661118 to your computer and use it in GitHub Desktop.
Save khacanh/5661118 to your computer and use it in GitHub Desktop.
Extensions for NSDictionary
#import "NSDictionary+Ext.h"
@implementation NSDictionary(Ext)
-(float)floatForKey:(NSString*)key
{
return [[self objectForKey:key] floatValue];
}
-(int)intForKey:(NSString*)key
{
return [[self objectForKey:key] intValue];
}
-(BOOL)boolForKey:(NSString*)key
{
return [[self objectForKey:key] boolValue];
}
+ (NSDictionary *) dictionaryByMerging: (NSDictionary *) dict1 with: (NSDictionary *) dict2 {
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:dict1];
[dict2 enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
[result setValue:obj forKey:key];
}];
return [result mutableCopy];
}
- (NSDictionary *) dictionaryByMergingWith: (NSDictionary *) dict {
return [[self class] dictionaryByMerging: self with: dict];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment