What does Block mean?
BlockIntroduction
Block usage scenarios can be used to pass values on two interfaces, or to encapsulate code as parameters. After using GCD, you will know the subtlety of the Block.
Block is a special data type. It can save a piece of code and call it when appropriate.
BlockModification
In the case of ARC
1. If you use copy to modify a Block, the Block will be stored in the heap space. The internal objects of the Block are strongly referenced, resulting in circular reference. Memory cannot be released.
Solution:
Create a pointer (_ weak typeof (Target) weakTarget = Target) pointing to the object in the Block code Block, and then operate with weakTarget. To solve the problem of circular reference.
2. If weak is used to modify the Block, the Block will be stored in the stack space. Loop reference is not required.
MRC
After being modified using copy, if you want to use objects inside the Block, you need to perform (_ block typeof (Target) blockTarget = Target) processing. Use blockTarget in the Block to perform operations.
Typedef int (^ SumP) (int, int); // use typedef to define a block type
// Use typedef to define a block to create a block Variable
SumP sumblock1 = ^ (int a, int B ){
Return a-B;
};
Int d = sumblock1 (10, 5 );
NSLog (@ "d = % d", d );
BlockDefinition Format
Return Value Type (^ block variable name) (parameter list) = ^ (parameter list ){
};
Call the code saved by Block
Block variable name (real parameter );
By default, BlockExternal local variables cannot be modified internally.
BlockInternal modification and use_ BlockModified local variables
Block.
1. No Block with no parameter or return value
2. Block with parameters without return values
3. Block with parameters and return values
BlockSimple usage example
Block with no parameter or return value
/**
* No Block with no parameter or return value
*/
-(Void) func1 {
/**
* Void: no return value.
* EmptyBlock: the name of the block.
* (): Put the parameter here. Because there is no parameter here, nothing is written.
*/
Void (^ emptyBlock) () = ^ (){
NSLog (@ "No parameter, no Block returned value ");
};
EmptyBlock ();
}
Block with parameters and no return values
/**
* Call this block to add two parameters.
*
* @ Param int parameter
* @ Param int parameter B
*
* @ Return no return value
*/
Void (^ sumBlock) (int, int) = ^ (int a, int B ){
NSLog (@ "% d + % d = % d", a, B, a + B );
};
/**
* Call the Block of the sumBlock and the result is 20.
*/
SumBlock (10, 10 );
Block with parameters and return values
/**
* Parameters and return values
*
* @ Param NSString string 1
* @ Param NSString string 2
*
* @ Return returns the spliced string 3.
*/
NSString * (^ logBlock) (NSString *, NSString *) = ^ (NSString * str1, NSString * str2 ){
Return [NSString stringWithFormat: @ "% @", str1, str2];
};
// Call logBlock. The output is Block.
NSLog (@ "% @", logBlock (@ "I am", @ "Block "));
BlockIntegrationTypedefUse
Define a Block type by yourself and create a Block with the defined type, which is simpler and more convenient.
Here is an example of a Block callback to modify the background color of the upper and lower bounds.
ViewController1 controller 1, ViewController2 controller 2
Controller 1 jumps to Controller 2, and then calls back the event triggered in controller 2 to change the background color of controller 1 to red.
Demo
ViewController2Implementation
# Import <UIKit/UIKit. h>
@ InterfaceViewController2:UIViewController
/**
*DefinesChangeColorOfBlock. ThisChangeColorA parameter must be included. The parameter type must beIdType
*No return value
* @ Param id
*/
TypedefVoid(^ ChangeColor )(Id);
/**
*UseChangeColorDeclareBlock,DeclaredBlockMust comply with the declared requirements.
*/
@ Property(Nonatomic,Copy) ChangeColor backgroundColor;
@ End
-(Void) TouchesBegan :(NSSet<UITouch*> *) Touches withEvent :(UIEvent*) Event {
//Declare a color
UIColor* Color = [UIColorRedColor];
//UseBlockGo to callback to modify the background color of the previous interface
Self. BackgroundColor (color );
}
ViewController1Implementation
-(Void) TouchesBegan :(NSSet<UITouch*> *) Touches withEvent :(UIEvent*) Event {
ViewController2 * vc = [[ViewController2 alloc] init];
//Modify color by callback
Vc. backgroundColor = ^ (UIColor* Color ){
Self. View. backgroundColor = color;
};
[Self. NavigationController pushViewController: vc animated: YES];
}