ios - Method that returns block -
i wrote method can used dot notation, using block tricks
- (raginidbquery *(^)(id object))where;
this method can called using dot notation since doesn't receive parameters, (it does, using block returned)
so can call so:
query.where(object)
what need instead getting id object
want receive in method:
- (nsenumerator *)where:(bool (^)(id object))predicate;
this method can called so:
[object where:^(id a){return [a intvalue]%2 == 1}];
what want combine both, calling method where
using dot notation, sending predicate block in example above, tried :
- (raginidbquery *(^)(bool (^)(id object))where;
but gives me errors, question this:
how can declare block, takes block?
edit:
for looking answer check below, here tl;dr
- (raginidbquery *(^)(bool(^filterblock)(id object)))where;
first of all, you're doing bad thing. should treat difficulty evidence it's misguided goal. objective-c isn't c# or java or swift. it's objective-c. if want use dot notation call functions on ios, use swift. don't try hammer objective-c other language's mold.
second, use typedef
s.
#import <foundation/foundation.h> typedef bool (^myobject_wherepredicate)(id object); typedef nsenumerator *(^myobject_wherereturn)(myobject_wherepredicate); @interface myobject: nsobject - (myobject_wherereturn)where; @end @implementation myobject - (myobject_wherereturn)where { return ^(myobject_wherepredicate predicate) { nsarray *array = @[ @"hello", @"world", @"this", @"is", @"a", @"test" ]; nspredicate *p = [nspredicate predicatewithblock:^bool(id evaluatedobject, nsdictionary *bindings) { return predicate(evaluatedobject); }]; return [array filteredarrayusingpredicate:p].objectenumerator; }; } @end int main(int argc, const char * argv[]) { @autoreleasepool { myobject *it = [[myobject alloc] init]; nsenumerator *e = it.where(^(id object){ nsstring *string = object; return [string containsstring:@"s"]; }); (nsstring *item in e) { nslog(@"%@", item); } } return 0; }
output:
2015-05-31 12:34:31.191 commandline[1881:82976] 2015-05-31 12:34:31.192 commandline[1881:82976] 2015-05-31 12:34:31.192 commandline[1881:82976] test
Comments
Post a Comment