Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Obj-C blocks are something I'm just using for the first time recently. I'm trying to understand the following block syntax:

In the header file:

@property (nonatomic, copy) void (^completionBlock)(id obj, NSError *err);

In the main file:

-(void)something{

id rootObject = nil;

// do something so rootObject is hopefully not nil

    if([self completionBlock])
        [self completionBlock](rootObject, nil); // What is this syntax referred to as?
}

I appreciate the assistance!

share|improve this question
    
IMO, it seems more clear what's going on if you use dot syntax. if( self.completionBLock) self.completionBlock(rootObject, nil); –  zpasternack Sep 21 '12 at 8:01

2 Answers 2

up vote 2 down vote accepted

Its a block property, you can set a block at runtime.

Here is the syntax to set

As it is void type, so within the class you can set a method by following code

self.completionBlock = ^(id aID, NSError *err){
    //do something here using id aID and NSError err
};

With following code you can call the method/block set previously.

if([self completionBlock])//only a check to see if you have set it or not
{
        [self completionBlock](aID, nil);//calling
}
share|improve this answer
    
Thank you very much! –  JaredH Sep 21 '12 at 16:15

Blocks are Objects.

In your case inside the method you are checking if the block is not nil and then you are calling it passing the two required arguments ...

Keep in mind that blocks are called in the same way a c function is ...

Below i have split the statement in two to let you understand better :

[self completionBlock]  //The property getter is called to retrieve the block object
   (rootObject, nil);   //The two required arguments are passed to the block object calling it
share|improve this answer
    
Thank you for the explanation! –  JaredH Sep 21 '12 at 16:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.