Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added NSDictionary filtering/mapping
- Loading branch information
1 parent
0bee516
commit bc2493e
Showing
6 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSDictionary+Derp.h | ||
// DerpKit | ||
// | ||
// Created by Steve Streza on 10/26/12. | ||
// Copyright (c) 2012 Mustacheware. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSDictionary (Derp) | ||
|
||
-(NSDictionary *)derp_dictionaryByMappingWithHandler:(id (^)(id object, id key, BOOL *stop))handler; | ||
-(NSDictionary *)derp_subdictionaryByFilteringWithHandler:(BOOL (^)(id object, id key, BOOL *stop))handler; | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// NSDictionary+Derp.m | ||
// DerpKit | ||
// | ||
// Created by Steve Streza on 10/26/12. | ||
// Copyright (c) 2012 Mustacheware. All rights reserved. | ||
// | ||
|
||
#import "NSDictionary+Derp.h" | ||
|
||
@implementation NSDictionary (Derp) | ||
|
||
-(NSDictionary *)derp_dictionaryByMappingWithHandler:(id (^)(id object, id key, BOOL *stop))handler{ | ||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:self.count]; | ||
|
||
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | ||
id object = handler(obj, key, stop); | ||
if(object){ | ||
[dictionary setObject:object forKey:key]; | ||
} | ||
}]; | ||
|
||
return [dictionary copy]; | ||
} | ||
|
||
-(NSDictionary *)derp_subdictionaryByFilteringWithHandler:(BOOL (^)(id object, id key, BOOL *stop))handler{ | ||
NSMutableDictionary *dictionary = [self mutableCopy]; | ||
|
||
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | ||
BOOL success = handler(obj, key, stop); | ||
if(!success){ | ||
[dictionary removeObjectForKey:key]; | ||
} | ||
}]; | ||
|
||
return [dictionary copy]; | ||
} | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ | |
#import "NSString+Derp.h" | ||
#import "NSData+Derp.h" | ||
#import "NSArray+Derp.h" | ||
#import "NSDictionary+Derp.h" |
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