Oolite
Loading...
Searching...
No Matches
OOFilteringEnumerator.h
Go to the documentation of this file.
1/*
2
3OOFilteringEnumerator.h
4By Jens Ayton
5
6NSEnumerator which takes an existing enumerator and filters out the objects
7that return NO from a given method. The method may take 0 or 1 arguments
8
9Example of use:
10 NSArray *cats = [self cats];
11 NSEnumerator *happyCatEnum = [[cats objectEnumerator] filteredWithSelector:@selector(isHappy)];
12 id happyCat = nil;
13
14 while ((happyCat = [happyCatEnum nextObject]))
15 {
16 ...
17 }
18
19Filters can be trivially chained. For instance, to get happy red cats, use:
20 NSEnumeratore *happyRedCatEnum = [[[cats objectEnumerator]
21 filteredWithSelector:@selector(isHappy)]
22 filteredWithSelector:@selector(hasColor:)
23 andArgument:[NSColor redColor]];
24
25Objects that do not respond to the filter selector are treated as if they had
26returned NO.
27
28Bonus feature: adds NSArray-like (but non-exception-throwing)
29makeObjectsPerformSelector: to all enumerators.
30
31
32Copyright (C) 2008-2013 Jens Ayton
33
34Permission is hereby granted, free of charge, to any person obtaining a copy
35of this software and associated documentation files (the "Software"), to deal
36in the Software without restriction, including without limitation the rights
37to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38copies of the Software, and to permit persons to whom the Software is
39furnished to do so, subject to the following conditions:
40
41The above copyright notice and this permission notice shall be included in all
42copies or substantial portions of the Software.
43
44THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50SOFTWARE.
51
52*/
53
54#import "OOFoundation.h"
55
56@interface OOFilteringEnumerator : NSEnumerator {
57@private
58 NSEnumerator* _underlyingEnum;
62}
63
64+ (id)filterEnumerator:(NSEnumerator*)enumerator withSelector:(SEL)selector;
65+ (id)filterEnumerator:(NSEnumerator*)enumerator withSelector:(SEL)selector andArgument:(id)argument;
66
67- (id)initWithUnderlyingEnumerator:(NSEnumerator*)enumerator
68 withSelector:(SEL)selector
69 takingArgument:(BOOL)takesArgument
70 argumentValue:(id)argument;
71
72@end
73
74@interface NSEnumerator (OOFilteringEnumerator)
75
76- (id)filteredWithSelector:(SEL)selector;
77- (id)filteredWithSelector:(SEL)selector andArgument:(id)argument;
78
79@end
80
81@interface NSArray (OOFilteringEnumerator)
82
83- (id)objectEnumeratorFilteredWithSelector:(SEL)selector;
84- (id)objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
85
86@end
87
88@interface NSSet (OOFilteringEnumerator)
89
90- (id)objectEnumeratorFilteredWithSelector:(SEL)selector;
91- (id)objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
92
93@end
94
95@interface NSDictionary (OOFilteringEnumerator)
96
97- (id)objectEnumeratorFilteredWithSelector:(SEL)selector;
98- (id)objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
99
100- (id)keyEnumeratorFilteredWithSelector:(SEL)selector;
101- (id)keyEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
102
103@end
104
105@interface NSEnumerator (OOMakeObjectsPerformSelector)
106
107- (void)makeObjectsPerformSelector:(SEL)selector;
108- (void)makeObjectsPerformSelector:(SEL)selector withObject:(id)argument;
109
110@end