-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCollectionsKeyValueFiltering.m
More file actions
61 lines (46 loc) · 1.24 KB
/
CollectionsKeyValueFiltering.m
File metadata and controls
61 lines (46 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// CollectionsKeyValueFiltering.m
//
//
// Copyright (c) 2010-2013 Nicolas Bouilleaud.
// http://bou.io/FilteringNSArrayWithKeyValueCoding
//
#import "CollectionsKeyValueFiltering.h"
@implementation NSArray (KeyValueFiltering)
- (id) firstObjectWithValue:(id)value forKeyPath:(NSString*)key
{
for (id object in self) {
if( [[object valueForKeyPath:key] isEqual:value] )
return object;
}
return nil;
}
- (NSArray*) filteredArrayWithValue:(id)value forKeyPath:(NSString*)key
{
NSMutableArray * objects = [NSMutableArray arrayWithCapacity:[self count]];
for (id object in self) {
if( [[object valueForKeyPath:key] isEqual:value] )
[objects addObject:object];
}
return [NSArray arrayWithArray:objects];
}
@end
@implementation NSSet (KeyValueFiltering)
- (id) anyObjectWithValue:(id)value forKeyPath:(NSString*)key
{
for (id object in self) {
if( [[object valueForKeyPath:key] isEqual:value] )
return object;
}
return nil;
}
- (NSSet*) filteredSetWithValue:(id)value forKeyPath:(NSString*)key
{
NSMutableSet * objects = [NSMutableSet setWithCapacity:[self count]];
for (id object in self) {
if( [[object valueForKeyPath:key] isEqual:value] )
[objects addObject:object];
}
return [NSSet setWithSet:objects];
}
@end