Think of it in terms of function Pointers

Block is also a data type (holds code blocks)

Define a block

No parameter no return value

void (^roseBlock) ();
// void: indicates that the block will save code without returning a value
// () : The code that represents the block will be saved has no parameters
// (^roseBlock) : reseBlock is a block variable that can be used to hold a block of code

roseBlock = ^(){
     NSLog(@"roseBlock");/ / roseBlock code block
};
// To execute the code saved by a block, you must call the block
roseBlock();

    
// If block takes no arguments, the () after ^ can be omitted
void (^jkblock)(void);
jkblock = ^{
    NSLog(@"jkblock code block");/ / jkblock code block
};
jkblock();
    
Copy the code

There are parameters and there are return values

int (^printBlock)(int);
// Block is a data type
printBlock = ^int (int num) {
    for (int i = 0; i < num; i++) {
        NSLog(@"----%d----",i);
    }
    return 1;
};

int result = printBlock(3);
NSLog(@" return value ----%d----",result);
Copy the code

Parameter has no return value

void (^block)(int) = ^void (int num) {
    NSLog(@"---%d--",num);/ / - 5 -
};
block(5);

void (^block1)(int) = ^ (int num) {
    NSLog(@"---%d--",num);/ / -- -- -- - 15
};
block1(15);
Copy the code

No argument, return value

NSString * (^block)(void);
//    block = ^NSString * (){  //写法1
// block = ^NSString * {// write 2
block = ^{  / / writing three
    return @"Jakcie";
};
NSString * name = block();//name == Jakcie
NSLog(@"name == %@",name);
Copy the code

Block Precautions

Blocks can access external variables

int a = 10;
void (^myBlock)(void) = ^ {NSLog(@"a = %d", a);//a = 10
};
myBlock();
Copy the code

By default, you cannot change the value of an external variable in a Block

  • A variable in a block is not the same variable as an external variable
  • When a block accesses an external variable, the block makes a copy of the external variable into heap memory
  • The external variables used in the block are copy, so changing the value of the external variable before the call does not affect the value of copy in the block
int a = 10;
NSLog(@"&a = %p", &a);//0x7ffeefbff4fc
void (^myBlock)(void) = ^ {//a = 50; Variable is not assignable (missing __block type specifier)
    NSLog(@"&a = %p", &a);//0x100662f70
    NSLog(@"a = %i", a);/ / 10
};
a = 20;
myBlock();
Copy the code

(_ _block) modifies variables that can be modified outside the Block

  • Do not add__blockIs direct transfer
  • add__blockAddress passing
 __block int a = 10;
NSLog(@" external &a = %p", &a);//0x7ffeefbff4f8
NSLog(@" external &a = %p", &a);//0x7ffeefbff4f8
void (^myBlock)(void) = ^{
    a = 50;
    NSLog(@" internal &a = %p", &a);//0x1004690c8
    NSLog(@" Internal a = % I", a);/ / 10
};
NSLog(@" external &a = %p", &a);//0x1004690c8
myBlock();
NSLog(@" external &a = %p", &a);//0x1004690c8
NSLog(@" external a = % I", a);/ / 50
Copy the code

Are blocks stored in a heap or on a stack?

  • By default, blocks are stored on the stack, and if you do a copy of the block, the block is moved to the heap

  • If the block is on the stack, the object is not retained from the block

  • However, if the block is in the heap and the block accesses an external object, a retain is performed on the external object

If you access an external object in a block, you must add a __block to the object. Once a __block is added, the object will not be retained even if the block is in the heap