Read all kinds of online articles about block, benefit a lot, just to sum up my own.

What are the three steps of knowing things? Why is that? How does it work?

###1. What is a block?

Block is an extension to C introduced in iOS4.0+ and Mac OSX 10.6+ to implement anonymous functions. The structure of a block is shown as follows:

<#returnType#>(^<#blockName#>)(<#parameterTypes#>) = ^(<#parameters#>) {

<#statements#>

};
Copy the code

The left side of the equation contains the return value type, block name, and parameter type, and the right side contains the parameter type, parameter name, and block return body.

###2. Why block?

Block is simple, easy to use and improves code utilization. Blocks can be used to pass values between pages or to reuse snippets of code

###3. How to use block

The three usages are as follows

####1) No return value No parameter

Void myHahaBlock (^myHahaBlock)() = ^{NSLog(@)"No entry, no return."); }; //2. Call block myHahaBlock(); }Copy the code

Output result:

Hahablock [75:25107] no entry, no return

####2) There is no return value for a parameter

Void (^myLolBlock)(int,int) = ^ (int num1,int num2){NSLog(@)"No return :%d",num1 + num2); }; //2. Call block myLolBlock(3,3); }Copy the code

Output result:

Hahablock [811:30160] :6

####3) There are parameters with return values

Double (^myHohoBlock)(double,double) = ^ (double r1,double r2){returnr1 + r2; }; //2. Call block double r3 = myHohoBlock(1.2,2.2); NSLog(@"Return :%f",r3);

}
Copy the code

Output result:

2016-08-20 11:06:42.615 Hahablock [861:32505] :3.400000

####4. Block is passed as a parameter

Refer to the http://www.jianshu.com/p/d28a5633b963 speak it well Don’t go into

Here by the way, I would like to talk about proxy transmission, quoting my answer on Baidu know:

The iOS Delegate design pattern is very common in OC or Swift. For example, the most common tableView delegate and datasource proxies are implemented so that their delegate methods can be used.

OK, have you thought about why? Why does the method we implemented in cellForRow change the contents of the cell?

For example, I have to clean my house every day, but I’m lazy, so I want to hire someone to clean my house, and a delegate comes along, and some beautiful young woman applies for the job. She set the delegate to self, so she cleaned my room every day (implement the proxy method), and the end result is that my room is clean and she uses my proxy method.

The purpose of a proxy method is to pass values, to put the values that the proxy object needs to pass into the proxy method, and when we get to a situation, we throw that value into our proxy method. Once someone proxies us as a proxy object, she gets the value we put into the proxy method, and the value is passed.

Code blocks, too, pass values between page jumps, usually backwards, for example

If you want to send A value to A on page B, go to Page B. We use the proxy by declaring the proxy at B, writing the proxy method, finding the place to pass the value, throwing the value into the proxy method. A page to achieve the proxy method B, take out the proxy method in the parameter also completed the value. Doesn’t it bother you? You have to declare the proxy and set up the proxy object. What about code blocks? First, again, declare a code block on page B and set it as a property. The values that need to be passed are then thrown into the code block. Check to see if the block exists. Code block directly when A pushes to B, by declaring the OBJECT B, you can obtain its properties, that is, you can get the code block in B, and then you can get the value in B, which completes the value transfer.

Very similar, right? I read a very succinct passage as follows:

So what does it make sense to define a Block in a class, specifically inside a -viewDidLoad method? I’m just going to say it’s just a private function at this point. As I said before, blocks are essentially proxies, so how can I compare them to proxies at this point? At this point I can say that a Block in this class is equivalent to the class obeying a protocol and then letting itself do something on its behalf.

Suddenly it was clear! When you understand a block of code like this, it’s instantly clear and exciting. Through the phenomenon to see the essence, abstract business, this is the need to learn and improve the place.

Next time, I’m going to talk about block memory leaks. See you next time!