Skip to content

Commit

Permalink
Add without to NSDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Apr 25, 2016
1 parent 1b16b3c commit d147569
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions NSDictionary+without.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import <Foundation/NSDictionary.h>

@interface NSDictionary (YOLOWithout)

/**
Returns a new dictionary where values for objects in the given array/set/object
are removed from the receiver.
id rv = @{@1:@"1", @2:@"2", @3:@"3", @4:@"4", @5:@"5", @6:@"6"].without(@2);
// rv => @[@1:@"1", @3:@"3", @4:@"4", @5:@"5", @6:@"6"]
id rv = @[@1:@"1", @2:@"2", @3:@"3", @4:@"4", @5:@"5", @6:@"6"].without(@[@2, @3]);
// rv => @[@1:@"1", @4:@"4", @5:@"5", @6:@"6"]
*/
- (NSDictionary *(^)(id arrayOrSetOrObject))without;

@end
21 changes: 21 additions & 0 deletions NSDictionary+without.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#import "YOLO.ph"

@implementation NSDictionary (YOLOWithout)

- (NSDictionary *(^)(id))without {
return ^NSDictionary *(id arrayOrSetOrObject) {
if (!arrayOrSetOrObject)
return self;

id objs = [arrayOrSetOrObject isKindOfClass:[NSSet class]]
? [arrayOrSetOrObject allObjects]
: [arrayOrSetOrObject isKindOfClass:[NSArray class]]
? arrayOrSetOrObject
: @[arrayOrSetOrObject];
id rv = self.mutableCopy;
[rv removeObjectsForKeys:objs];
return rv;
};
}

@end

0 comments on commit d147569

Please sign in to comment.