Basic usage of Block in IOS


Source: http://www.jianshu.com/p/e03292674e60

Introduction
to Block Block is a special data type. It can save a piece of code and retrieve it and call it when appropriate.


In the case of ARC
1. If the Block is modified with copy, the Block will be stored in the heap space. A strong reference will be made to the internal object of the Block, resulting in a circular reference. Memory could not be freed.
Solution:
Create a new pointer (__weak typeof(Target) weakTarget = Target ) to point to the object in the Block code block, and then use weakTarget to operate. The circular reference problem can be solved.

2. If the Block is modified with weak, the Block will be stored in the stack space. There will be no circular reference problem.

In the case of MRC After modifying
with copy, if you want to use the object inside the Block, you need to perform (__block typeof(Target) blockTarget = Target ) processing. Use blockTarget to operate in Block.




Block definition format
Return value type (^block variable name) (formal parameter list) = ^ (formal parameter list) {
};
call the
block variable name (actual parameter) of the code saved by the block; by

default, the inside of the block cannot be modified outside The local variable Block can modify the mode
of the local variable Block modified with __block 1. Block with no parameters and no return value



2. Block with parameters but no return value
3. Block


usage example with parameters and return value

Block with no parameters and no return value

/**
 * Block with no parameters and no return value
 */
-(void)func1{
    /**
     * void : means no return value
     * emptyBlock: is the name of the block
     * (): This is equivalent to putting parameters. Since there are no parameters here, nothing is written
     */
    void (^emptyBlock)() = ^(){
        NSLog(@"Block with no parameters and no return value");
    };
    emptyBlock();
}
Block with parameters but no return value

/**
     * Call this block to add two parameters
     *
     * @param int parameter A
     * @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 this sumBlock, and the result is 20
     */
    sumBlock(10,10);
Block with parameters and return value

/**
     * There are parameters and return values
     *
     * @param NSString string1
     * @param NSString string 2
     *
     * @return returns the concatenated string 3
     */    
    NSString* (^logBlock)(NSString *,NSString *) = ^(NSString * str1,NSString *str2){
        return [NSString stringWithFormat:@"%@%@",str1,str2];
    };
    //Call logBlock, the output is that I am Block
    NSLog(@"%@", logBlock(@"我是",@"Block"));


Using Block in combination with typedef It is simpler and more convenient to define a Block type by
yourself , and use the defined type to create a Block.
Here is an example of a Block callback to modify the background color of the previous interface.
ViewController1 Controller 1, ViewController2 Controller 2
Controller 1 jumps to Controller 2, and then triggers an event callback in Controller 2 to modify the background color of Controller 1 to red.

Implementation of ViewController2

#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
/**
 * Defines a block of changeColor. This changeColor must take a parameter, the type of this parameter must be of type id
 * no return value
 *  @param id
 */
typedef void(^changeColor)(id);
/**
 * Declare a Block with the changeColor defined above, and the declared Block must comply with the declared requirements.
 */
@property (nonatomic, copy) changeColor backgroundColor;
@end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //declare a color
    UIColor *color = [UIColor redColor];
    //Use the block just declared to call back to modify the background color of the previous interface
    self.backgroundColor(color);
}
Implementation of ViewController1

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    ViewController2 *vc =[[ViewController2 alloc]init];
    // callback to modify color
    vc.backgroundColor = ^(UIColor *color){
        self.view.backgroundColor = color;
    };
    [self.navigationController pushViewController:vc animated:YES];
}





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614157&siteId=291194637