Oolite 1.91.0.7604-240417-a536cbe
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 "OOCocoa.h"
55
56
57@interface OOFilteringEnumerator: NSEnumerator
58{
59@private
60 NSEnumerator *_underlyingEnum;
64}
65
66+ (id) filterEnumerator:(NSEnumerator *)enumerator withSelector:(SEL)selector;
67+ (id) filterEnumerator:(NSEnumerator *)enumerator withSelector:(SEL)selector andArgument:(id)argument;
68
69- (id) initWithUnderlyingEnumerator:(NSEnumerator *)enumerator
70 withSelector:(SEL)selector
71 takingArgument:(BOOL)takesArgument
72 argumentValue:(id)argument;
73
74@end
75
76
77@interface NSEnumerator (OOFilteringEnumerator)
78
79- (id) filteredWithSelector:(SEL)selector;
80- (id) filteredWithSelector:(SEL)selector andArgument:(id)argument;
81
82@end
83
84
85@interface NSArray (OOFilteringEnumerator)
86
87- (id) objectEnumeratorFilteredWithSelector:(SEL)selector;
88- (id) objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
89
90@end
91
92
93@interface NSSet (OOFilteringEnumerator)
94
95- (id) objectEnumeratorFilteredWithSelector:(SEL)selector;
96- (id) objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
97
98@end
99
100
101@interface NSDictionary (OOFilteringEnumerator)
102
103- (id) objectEnumeratorFilteredWithSelector:(SEL)selector;
104- (id) objectEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
105
106- (id) keyEnumeratorFilteredWithSelector:(SEL)selector;
107- (id) keyEnumeratorFilteredWithSelector:(SEL)selector andArgument:(id)argument;
108
109@end
110
111
112@interface NSEnumerator (OOMakeObjectsPerformSelector)
113
114- (void)makeObjectsPerformSelector:(SEL)selector;
115- (void)makeObjectsPerformSelector:(SEL)selector withObject:(id)argument;
116
117@end