Custom completion block for my own method

user2206906 picture user2206906 · May 1, 2013 · Viewed 67.9k times · Source

I have just discovered completion blocks:

 completion:^(BOOL finished){


                     }];

What do I need to do to have my own method take a completion block?

Answer

Thilina Chamin Hewagama picture Thilina Chamin Hewagama · May 1, 2013

1) Define your own completion block,

typedef void(^myCompletion)(BOOL);

2) Create a method which takes your completion block as a parameter,

-(void) myMethod:(myCompletion) compblock{
    //do stuff
    compblock(YES);
}

3)This is how you use it,

[self myMethod:^(BOOL finished) {
    if(finished){
        NSLog(@"success");
    }
}];

enter image description here