1、Block是什么? - 匿名函数 - 截获自动变量2、Block语法。3、Block类型变量。4、Block的用途。 - 作为函数参数 - 反向传值 - 循环引用(delegate差不多作用,但是显得更加简洁)
First, what is block? an anonymous function with an automatic variable is summed up in a sentence.
Then we explain what is "anonymous function", what is "automatic variable", then I believe you probably have a general understanding of Block .
- anonymous functions
Anonymous functions as the name implies is a function without a name, in the C language does not allow such a method exists, and in OC block can be used to directly invoke a pointer to a function, but we still need to know the name of the pointer. (On this point, the amount ~ ~ we still do not tangle is better.) ! ~ ~)
- Automatic variables
The specific behavior of the automatic variable in block is to intercept the automatic variable , and take a look at the following code:
int b = 0; void (^blo)() = ^{ NSLog(@"Input:b=%d",b); }; b = 3; blo(); /** * Input:b=0 */
Although we changed the value of B before calling Blo, but the output is the value of the Block compile time B, so the intercept instantaneous automatic variable is: The value of the variable will be saved in the block, and will not change with the value of the variable.
Let's look at some code.
int b = 0; void (^blo)() = ^{ b = 3; };
This code compiles an error, and the compiler hints that the value of the variable cannot be changed in the Block . Since the value of the variable can no longer be changed after intercepting the instantaneous value of the variable in the block , if you want to change the value of the variable in the block , then we just need to add the __block modifier to the variable declaration, like this:
__block int b = 0; void (^blo)() = ^{ b = 3; };
However, such a situation is permissible:
NSMutableArray *array = [[NSMutableArray alloc]init]; void (^blo)() = ^{ [array addObject:@"Obj"]; };
Why, because we're just manipulating the intercepted variable, and not assigning it, so for the intercept variable, you can manipulate it and not be able to assign a value.
It is also important to note that the C-language array cannot be manipulated in Block because: ~ ~ ~ Not supported ....
With anonymous functions and the ability to intercept automatic variables,Block can do a lot of things, and we're looking underneath.
Let's take a concrete look at block syntax writing, and we'll start with a complete block:
^ NSString *(NSString *a,NSString *b){ return a; };
Let's explain separately what each of these parts is:
- The symbol "^" indicates that this is a block;
- NSString * indicates the return value.
- (NSString a,nsstring b) The parentheses are the parameters of the block, and the syntax is similar to the C language.
In fact, we can omit the return value of block, write it like this:
^ (NSString *a,NSString *b){ return a; };
This writing is exactly the same as the one above, in fact, if there is no parameter list, we can even omit the argument list, like this:
^ { NSLog(@"我没有参数列表"); };
If this code is complete, then this is the case:
^void(void) { NSLog(@"我没有参数列表"); };
Why do I need a block variable? We can understand this by using the block variable to get a pointer to the block, and then we can use the block function with this pointer. Let's start by looking at how to declare a block variable
int (^Blo)(NSString *s1,NSString *s2);
Compared with the previous block function, we can easily understand the meaning of each part:
They were:
- return value
- Variable name
- Parameter list
OK, then we'll use the block syntax above to assign the block variable:
int (^Blo)(NSString *s1,NSString *s2); Blo = ^(NSString *s1,NSString *s2){ return 1; };
Then we can use this block variable as a C-language function.
So what does block do with it?
Block can be used as a function parameter, first we declare a block type variable, and add a typedef modifier, like this:
typedef void(^Blo)(NSString *s1,UIColor *c);
So that we can use Blo to represent the block, and then I can add Blo to the function arguments, and we declare a function:
-(void)func:(Blo)BlockPra{ BlockPra(@"Str",[UIColor redColor]);}
Then we can use this function like this:
[self func:^(NSString *s1, UIColor *c) { NSLog(@"%@",s1); self.view.backgroundColor = c; }];
Do not feel very familiar, usually used in many of the callbacks are mostly in this form, perhaps more of it is the network callback, we only need to call the method, and then in the callback can be manipulated in the results, many Apple's own API is used to write this method, The advantage of this is that the form of 10 concise, of course, like this place you use delegate certainly can be, but the performance is not block so concise, the use of the block so convenient.
In addition, block can also be used as a communication between controllers.
We already know that Blcok is an anonymous function and a pointer, so using block can compensate for functions passed in iOS. This is usually the way it is used:
This block is defined in the. h file of page B, and then a variable is declared, like this:
typedef void(^Blo)(NSString *s1,UIColor *c); @property (nonatomic, copy) Blo block;
Then we have this piece of code in page A:
ViewController *b = [[ViewController alloc]init]; __weak ViewController *wself = self; b.block = ^(NSString *s1,UIColor *c){ NSLog(@"%@",s1); wself.view.backgroundColor = c; }; [self.navigationController pushViewController:b animated:true];
Then we call the block variable anywhere in page B, like this:
self.block(@"str",[UIColor redColor]);
will be in a page to invoke the B page passed the parameters, on the A page operation, the controller a changes, this practice is usually used as a controller reverse value.
One thing to note here is the circular reference caused by the use of the block. If the object type automatic variable with the __strong modifier is used in the block, the object is block-owned when the block is copied from the stack to the heap. This can cause a circular reference, which causes a memory leak, but we just need to make sure that the current controller, self, is released correctly when it needs to be released, so let's look at the code above:
__weak ViewController *wself = self;
We define a wself variable and add the __weak modifier, and in block code, all the places that need self are replaced with wself . This will not increase the reference count, so the block holding the self object will not cause a circular reference, resulting in a memory leak.
Whether the block is used as a function parameter or a reverse value, it is all about the nature of the block , the "anonymous function with automatic variables", the two modifiers, "with the automatic variable", "anonymous function" two characteristics of the application.
Summarize what block is and what it is for:
- A struct in C + + (not mentioned in this article).
- Used to compensate for function passing in iOS.
- He is a pointer to the memory of a block of code.
- The same functions as delegate, but more concise.
Strongx
Links: http://www.jianshu.com/p/ce238bd58d5b
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Use of Ios-block