There are a lot of block articles on the Internet that are used only to record the points that you think are important.
Block essence is an object that involves the concept of closures for encapsulating code
int (^block) (int A,int b) = ^ (int A,int b) { return a + B; };
Definition: Int (^block) (int a,int b)
Parameters: Int return type
int a output parameter a (name can not be written)
int b input parameter B ( name can not be written)
Implementation: ^ (int A,intreturn a + b;};
parameter: int A,int B (name is not missing)
{} implementation
The block is stored in a different type, and these types are only used by the compiler
1, _nsconcretestackblock (nsstackblock) stored on the stack;
2, _nsconcreteglobalblock (nsglobalblock) stored in the code snippet of the program;
3, _nsconcretemallocblock (nsmallocblock) is stored on the heap.
Knowledge Tip: Program code area (text), Global zone (static), constant area, stack (stack), heap area (heap, using malloc) are stored in the C language
Description
- Nsglobalblock: No external variables are referenced inside the block
- Nsstackblock: An external variable is referenced inside the block, and the block on the stack exits with the stack and references cause a crash.
- Nsmallocblock: Increased number of pointers used outside of its role (copy, Rerain)
Note: In the ARC case, there is no nsstackblock this type, if there is a system auto copy becomes Nsmallocblock, while copying code internal variables such as:
int base = 2;
Base + = 2; Base->4
Long (^sums) (int,int) = ^ long (int a,int b) {
Return base + A + B; Base->4
};
Base + +; Base->5
NSLog (@ "%ld", sums);
The result of the output is a pre-copy base of 7 arc
Classic case, bring your own answer: http://blog.parse.com/learn/engineering/objective-c-blocks-quiz/
Third, citing the problem: only Nsmallocblock support retain, release operation (the cliché of the specific self-search it)
1, block internal code modification data situation: __block decoration, static
2, circular reference problem: Block code said reference, using __weak typeof (self) weakself = self;
3, block objects are released early: do not use __block
iOS block type