Objective-C Blocks - Use as an Object

As for the code below, once the block has been placed in an array, how can you take this block object and run the actual code in the block.

Another question: if I call a method in a block, as shown below, will this block encapsulate the code in this method or capture the signature of the method and call it this way?

-(void)blockCalledMethod { NSLog(@"Hello World"); } -(void)programStart { NSArray * array = [[NSArray alloc] initWithObjects:[[^ { [self blockCalledMethod];} copy] autorelease],nil]; id pointerToBlock = [array lastObject]; } 
+4
source share
1 answer

Name it as follows:

 void (^pointerToBlock)(void) = [array lastObject]; pointerToBlock(); // because ^ {} is void(^)(void) 

You cannot declare pointerToBlock as id if you want to call it directly, because the compiler must recognize it as a block type, not just an object.

If I call a method in a block, as shown above, does this block block the code in this method or write the signature of the method and call it this way?

I think self refers to the calling class.

+7
source

Source: https://habr.com/ru/post/1335413/


All Articles