For training (but not practical) purposes, I would like to use the following method in NSDictionary to return to me a set of keys that have values ββusing the test I defined. Unfortunately, I donβt know how to specify a predicate.
NSDictionary keysOfEntriesPassingTest: - (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(id key, id obj, BOOL *stop))predicate
Say, for example, all my values ββare NSURL, and I would like to return all the URLs that are on port 8080. Here's my hit on coding, although it doesn't seem to me any sense that it's' d Correct:
NSSet * mySet = [myDict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) { if( [[obj port] isEqual: [NSNumber numberWithInt: 8080]]) { return key; }]
And this is because I am returning the following compiler error:
incompatible block pointer types initialization 'void (^) (struct objc_object *, struct objc_object *, BOOL *)', expected 'BOOL (^) (struct objc_object *, struct objc_object *, BOOL *)'
What am I missing? I would be grateful for a pointer to some documents that describe in more detail about the "block object", which is supposed to be a predicate .
Thank!
And this is the code that works:
NSSet *mySet = [myDict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) { if ([[obj port] isEqual:[NSNumber numberWithInt: 8080]]) return YES; else return NO; }];
cocoa grand-central-dispatch macos
Todd Jun 13 '10 at 23:11 2010-06-13 23:11
source share