Objective-C Block Syntax

Obj-C blocks are what I use for the first time lately. I am 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 help!

+5
source share
2 answers

The block property, you can set the block at run time.

Here is the syntax to install

Since it is a void type, so inside the class you can set the method by specifying the code

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

Using the following code, you can call a previously set method / block.

if([self completionBlock])//only a check to see if you have set it or not
{
        [self completionBlock](aID, nil);//calling
}
+2
source

Blocks are objects.

In your case, inside the method, you check to see if the block is blocked, and then you call it by passing two necessary arguments ...

Keep in mind that blocks are called just like the c ... function

, :

[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
+5

All Articles