IOS development-Grammar-block explanation, ios development-block explanation

Source: Internet
Author: User

IOS development-Grammar-block explanation, ios development-block explanation

I. Basic Definition

 

/* Some preliminary understandings and explanations of block definitions will be explained in the following sections:

* The block name is myBlock. In combination with the function pointer of C, myBlock is the pointer of the block body and points to the entry address of the block body.

* Int result = myBlock (5) <=> ^ (int num) {return num * num} (5) // pass 5 to num

* You can use myBlock as a parameter during callback, or directly input the block body ^ (int num ){...}; //

* When the entire block body is passed in as a parameter, there is usually no parameter, but it is only used for latency calculation, because the block content is defined.

If the left value is not required for the moment, the block is not executed.

Example: [request setFailedBlock: ^ {NSError * error = [request error] ;};];

// If the setFailedBlock method is not executed, the subsequent block will not be executed, but will be quietly waiting in the heap. The principle will be discussed later.

*/

Ii. block Principle

First, we will introduce the basic block and rewrite the OC block into a C block.

Procedure:

We can see from the definition of IMP:

In the main () function, the block return value is (void (*) (), which is actually a function pointer. In this case, block can be treated as the function pointer of an anonymous function.

3. Keyword _ block

Main functions: 1. block is read-only for external variables. To become readable and writable, add _ block.

2. Copy the block in the stack to avoid circular reference.

Convert a block with the keyword _ block to OC-> C:

Iv. Storage Type

There are three types of block Storage: _ NSConcretStackBlock (stack), _ NSConcretGlobalBlock (global), and _ NSConcretMallocBlock (HEAP)

Key Aspect 1:When the block is inside the function and the variable inside the function is used for definition, the block is stored on the stack.

Key Aspect 2:When the block is defined outside the function body or inside the function body and the function is executed at that time, the block body does not need to use local variables inside the function, that is, when the block function is executed, it is quietly waiting for a while to define the content without using the function body. Then the block will be stored as a global block by the compiler.

Key Aspect 3:The global block is stored in the heap. The original function pointer is returned when the copy operation is performed on the global block. The copy operation on the block in the stack generates two different block addresses, that is, the entry address of two anonymous functions.

Key Aspect 4: ARC Mechanism Optimization converts stack blocks into heap blocks for calling.

The following storage questions are taken from the cocoaChina Forum:

 

 

Related Article

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.