iOS block type

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.