The ios4.0 System has started to support block, and during programming, blocks is considered an object by Obj-c, which encapsulates a piece of code that can be executed at any time. Blocks can be used as a function parameter or as a return value of a function, and it can have an input parameter or return value itself. It is similar to a traditional function pointer, but differs: blocks is inline, and it is read-only to local variables.
Here is the theoretical part:
1. Definition of block
1 //declarations and implementations are written together, like declarations of variables that implement int a = ten;2 int(^ablock) (int,int) = ^(intNUM1,intnum2) {3 4 returnNUM1 *num2;5 6 };7 //declarations and implementations are separated, as if the variables were first declared after the implementation of int a;a = ten;8 int(^cblock) (int,int);9Cblock = ^ (intNUM1,intnum2)Ten { One returnNUM1 *num2; A};
It defines a blocks object named Ablock and carries the relevant information:
1, Ablock has two formal parameters, respectively, the type of int;
2, the return value of ablock is int type;
3, the right side of the equation is the concrete realization of blocks;
4, ^ with edge blocks declaration and implementation of the logo (keywords);
Of course, you can define other forms of block. E.g: no return value, no formal parameters, etc.;
1 void (^bblock) () = ^()2 {3 int10 ; 4 printf ("num =%d", a); 5 };
2. Blocks access rights
Blocks can access local variables, but cannot be modified.
1 intten; 2 int (^dblock) (int) = ^ (int num)3 {4 a++; not work! 5 return num * A; 6 };
The reason why cannot be modified here is determined during compilation, when the compiler compiles the value of a to block as a new variable (assuming a ' = 10), at which point a ' and a are not related.
This is where the value in the function is passed. Add keyword if you want to modify: __block or static
1 int7; 2 int (^dblock) (int) = ^ (int num)3 {4 a++; work! 5 return num * A; 6 };
3. Call of block
Block calls are just like calling functions. e.g:
| 1 |
intc = aBlock(10,10); <br> bBlock(); |
4. Block Application
Suppose we are familiar with the proxy recursion value, we may love and hate the agent! We first set up the Model a page Push b page, if the value of a page passed to the B page, properties and single-pass value can be done! But if the pop process to pass the value of the B page to a page, then you can use a single case or agent! When it comes to proxies, we have to declare the agreement and create the agent, which is very troublesome. Often we pass a value that requires a lot of code to be written between two pages, which changes the overall order of the pages and the readability is discounted. So, at this point, block is an optimization scheme!
5, block of memory management
block itself is like an object can be retain, and release. However, when the block is created, its memory is allocated on the stack (stack), not on the heap (heap). His own domain is the scope at the time of creation, and once it is created, calling block will cause the program to crash. Like the example below. I created a block in view did load:
- -(void) viewdidload
- {
- [Superviewdidload];
- int number = 1;
- _block = ^ () {
- NSLog (@ "number%d", number);
- };
- }
and this block is called in the event of a button:
- -(Ibaction) Testdidclick: (ID) Sender {
- _block ();
- }
At this point I press the button will cause the program crashes, the solution to this problem is to create a block after the need to call the Copy method. Copy will move the block from the stack to the heap, so you can use the block somewhere else.Modify the code as follows:
- _block = ^ () {
- NSLog (@ "number%d", number);
- };
- _block = [_blockcopy];
In the same way, it is important to note that when the block is placed in the collection class, if the generated block is placed directly into the collection class, it is not possible to use the block elsewhere, you must copy the block. But the code looks relatively odd:
- [Array addobject:[[^{
- NSLog (@ "hello!");
- } copy] [autorelease]];
6. Circular reference
for non-arc, to prevent circular referencing, we use __block to decorate the objects used in the block:
For arc, to prevent circular referencing, we use __weak to decorate the objects used in the block. The principle is: in Arc, if the __strong modifier's automatic variable is referenced in the block, it is equivalent to the block's reference count of that variable +1.
This is actually a small derivative in the 1th. When using member variables within a block, such as
- @interface Viewcontroller:uiviewcontroller
- {
- NSString *_string;
- }
- @end
In block creation:
- _block = ^ () {
- nslog (@" string %@ ", _string);
- };&NBSP;
The _string here is the equivalent of self->_string;, so block is a retain to the internal object. In other words, self will be retain once. When Self is released, it is necessary to release the self after the block is released, but the block's release needs to wait for Self's dealloc to be released. As a result, a circular reference is formed, causing a memory leak.
The modification scenario is to create a new local variable for the __block scope and assign self to it, and the local variable is used inside the block to take the value. Because the __block tag variable is not automatically retain.
- __block Viewcontroller *Controller = self;
- _block = ^ () {
- NSLog (@ "string%@", controller->_string);
- };
iOS block detailed usage