Block Object

I do not know how to translate Block keyword in Objective-C, but it is certainly too: Block is a piece of code.

We see it in English, explained:

Blocks are a way to define a block of code that you will use at a later time.

Sometimes people refer to blocks as anonymous functions because they are functions that aren’t
attached to an entity.

In fact, Block somewhat similar to our C # or Java in the anonymous function.

 

Look at an example: 

eg:

squareThis = ^(float x){
  return x * x;
};

 

float result = squareThis(4);
NSLog(@"result = %f", result);

This will print out the following output to the console log:
result = 16.000000

NSString *title = @"Multiply Block Execution";
float (^multiplyThese)(float, float) = ^(float x, float y)
{
    NSLog(title);

    return x * y;
};

 

NSLog(@"multiplyThese(3,4) = %f", multiplyThese(3,4));

Output:

Multiply Block Execution
multiplyThese(3,4) = 12.000000

 

 

 

 

 

Reproduced in: https: //www.cnblogs.com/davidgu/p/3816284.html

Guess you like

Origin blog.csdn.net/weixin_34357962/article/details/93802946