Introduction to this article
This chapter does not do much research on block implementations. Just explain the basic usage. Purely basic knowledge. The actual project how to do the example. Block using the scene, can be in the two interface of the value, you can also code encapsulation as a parameter of the pass, and so on. Use GCD to know the subtleties of the block.
About Block
Block is a very special type of data. It can save a piece of code and fetch it at the appropriate time to invoke it.
The modification of block
In the case of arc
1. If the block is modified with copy, the block is stored in the heap space. The internal object of the block is strongly referenced, resulting in a circular reference. Memory cannot be freed.
Workaround:
Create a new pointer (__weak typeof (target) Weaktarget = target) and point to the object in the block code, and then use Weaktarget to do so. You can solve circular reference problems.
2. If the block is modified with weak, the block will be stored in the stack space. There is no circular reference issue.
MRC Case
With copy retouching, if you want to use objects inside a block, you need to process (__block typeof (target) Blocktarget = target). Operate with Blocktarget in block.
Block's definition Format
return value type (^block variable name) (parameter list) = ^ (parameter list) {
};
Calling block-saved code
Block variable name (argument);
By default, the outer local variables cannot be modified inside the block
Local variables modified using __block can be changed inside the block
Block's mode
1. Block without parameters and no return value
2. Block with parameter no return value
3. Block with parameters and return values
Example of block simple usage
Block with no parameter and no return value
/** * 无参数无返回值的Block */-(void)func1{ /** * void :就是无返回值 * emptyBlock:就是该block的名字 * ():这里相当于放参数。由于这里是无参数,所以就什么都不写 */ void (^emptyBlock)() = ^(){ NSLog(@"无参数,无返回值的Block"); }; emptyBlock();}
Block with parameter no return value
/** * 调用这个block进行两个参数相加 * * @param int 参数A * @param int 参数B * * @return 无返回值 */ void (^sumBlock)(int ,int ) = ^(int a,int b){ NSLog(@"%d + %d = %d",a,b,a+b); }; /** * 调用这个sumBlock的Block,得到的结果是20 */ sumBlock(10,10);
Block with parameters with return value
/** * 有参数有返回值 * * @param NSString 字符串1 * @param NSString 字符串2 * * @return 返回拼接好的字符串3 */ NSString* (^logBlock)(NSString *,NSString *) = ^(NSString * str1,NSString *str2){ return [NSString stringWithFormat:@"%@%@",str1,str2]; }; //调用logBlock,输出的是 我是Block NSLog(@"%@", logBlock(@"我是",@"Block"));
Block combined with typedef
It is simpler and more convenient to define a block type by itself and create blocks with defined types.
Here, for example, a block callback modifies the background color of the previous interface.
ViewController1 Controller 1,viewcontroller2 Controller 2
Controller 1 jumps to controller 2, and then the Controller 2 triggers an event callback to modify the background color of controller 1 to red.
The realization of ViewController2
#import <uikit/uikit.h> @interface ViewController2: uiviewcontroller/** * defines a changecolor block. This changecolor must take a parameter, the type of the parameter must be an ID type of * no return value * @param ID */typedef void (^changecolor) (id); /** * Using the ChangeColor defined above to declare a block, the block declared must comply with the requirements of the Declaration. */ @property (nonatomic, copy) ChangeColor BackgroundColor; @end
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //声明一个颜色 UIColor *color = [UIColor redColor]; //用刚刚声明的那个Block去回调修改上一界面的背景色 self.backgroundColor(color);}
The realization of ViewController1
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ ViewController2 *vc =[[ViewController2 alloc]init]; // 回调修改颜色 vc.backgroundColor = ^(UIColor *color){ self.view.backgroundColor = color; }; [self.navigationController pushViewController:vc animated:YES];}
Text/Like a child (Jane book author)
Original link: http://www.jianshu.com/p/17872da184fb
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
Basic usage of block in iOS