Introduction to iOS development-block

Source: Internet
Author: User

A block called a closure, is actually a block, the name of the block is similar to the entry address of the function, has its own scope, after the scope of the end of acceptance is destroyed (usually after the function ends), is usually allocated in the stack (to access local variables and improve performance), The block itself is an object in Objective-c, and for memory management methods, we can use blocks to complete some of the program fragments that are executed synchronously, encapsulating some small work and callbacks (callback) when a job is completed. Blocks , as a practical alternative to traditional callback functions, has the following two reasons:

1. They allow you to write code where it is called to implement the actions that will be performed later. Therefore , Blocks is usually used as a parameter to the framework method.

2. They allow you to access local variables. Instead of having to use a data structure that integrates all the context information you want to perform, you can simply access the local variables directly.

Common wording for 1.Block

1. Declare a block and its implementation

void (^my_block) (void) =^ (void) {

NSLog ("my Block");

}

My_block ();

2. You can define a block type

typedef void (^my_block_type) (void)

My_block_type My_block = ^ (void) {

NSLog ("my Block");

}

2.Block Access to variables

By default, the local variables used by the block are copied, not reserved. So it cannot change the value of a local variable.

If you add __block to a variable, the compiler does not copy the variable, but instead goes to the address of the variable and accesses the variable through the address, which is actually the direct manipulation of the variable.     The other block is allocated on the stack, so once you leave the scope, it will be released, so if you want to use it somewhere else, you have to copy it.     So you need to use copy: @property (nonatomic, copy) void (^my_block_type) (void) When a property definition is fast; Blocks are not preserved, and retain does not make sense for blocks.

3.Block is a structural data structure in the underlying implementation.

The corresponding structure is defined as follows:

struct block_descriptor {

unsigned long int reserved;

unsigned long int size;

void (*copy) (void *DST, void *src );

void (*dispose) (void *);

};

struct block_layout {

void *ISA;

int flags;

int reserved;

void (*invoke) (void *, ...);

struct block_descriptor *descriptor;

/ * Imported variables. * *

};


With this diagram, we can see that a block instance is actually composed of 6 parts:

1. TheISA pointer, which all objects have that pointer, is used to implement object-related functionality.

2.flags, used to represent some additional information about the block by bit bit , the implementation code of block copy later in this article can see the use of the variable.

3.reserved, reserved variables.

4.invoke, function pointer, point to the specific block implementation of the function call address.

5.descriptor, which represents the additional descriptive information for the block, mainly size sizes, and pointers to the copy and dispose functions.

6.variables,capture the variable,block can access its external local variables, because the variables (or the address of the variable) are copied into the structure.

4. The difference between Arc and non-arc block:

when the Block of ARC is automatically copied from the stack to the push , as well as The use of __block and __weak

Since block is built by default on the stack , If you leave the method scope , the block will be discarded, and in non- ARC cases , We want to return a block, need [block copy];

under ARC , the Block is automatically copied from the stack to the heap in the following cases:

1. The copy method is executed

2. return value as method

3. Assigning a Block to a class with an ID type with __strong modifier or blcok type member variable

4. when the method name contains the usingblock Cocoa Framework method or the GDC API is passed .

For non- ARC , in order to prevent circular referencing , we use __block to decorate the objects that are useful in block :

__block ID blockself=self;

self.blk=^{

NSLog (@ "%@", blockself); under non- ARC for _block objects on the stack, the block does not copy it, and the reference count is not incremented for use only.

};

For ARC , in order to prevent circular referencing , we use __weak to decorate the objects that are useful in Block :

__weak ID weakself=self;

self.blk=^{

NSLog (@ "%@", weakself);

};

Introduction to iOS development-block

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.