2

I am going around blocks and try to discover the ways that they can be used. So I am wondering is it possible to pass block to block like parameter?

Here is some sample code:

//declaration
static id (^someBlock)(id) = ^(id someClass) {
    // do some stuff to obtain class some class instance
    // check if class instance respond to @selector 
    // if yes - perform selector
}

//usage
+ (instancetype)someMethod {
   someBlock(SomeClass.class);
   // do additional work and return some instance type
}

This works fine, but is not good enough, because we obligate caller to respond to selector if caller want to do some additional stuff when someBlock is completed.

So my question is how I can invoke someBlock block with parameter block which I want to be executed when someBlock is completed.

Some like:

    //declaration
    static id (^someBlock)(id, <b>^otherBlock</b>) = ^(id someClass, <b>????</b>) {
        // do some stuff to obtain class some class instance
        otherBlock();
    }

Any advice?

PS: Please note that the question is not about passing block to method as parameter.

Thanks, Venelin

2
  • Is otherBlock always the same, or do you want it to be any block? If it's the same, you can just define it, and always call it in the end of someBlock. Commented Dec 13, 2013 at 19:04
  • Thanks, I will get this in my mind, it is useful. Commented Dec 13, 2013 at 20:08

1 Answer 1

2

Is this what you are looking for?

static id (^someBlock)(id, void (^otherBlock)()) = ^id (id someClass, void (^otherBlock)()) {
    otherBlock();
    return nil; // just because you declares a `id` return type
};

And call it like

someBlock(someClass, ^() {
    NSLog(@"other stuff");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.