Objective-c (use of block)

Source: Internet
Author: User

Block

Block is used to save a piece of code

Block's logo: ^

Blocks are like functions: You can save code, return values, physical arguments, call methods as you call them.

External variables can be accessed inside the block

By default, the outer local variables cannot be modified inside the block

Add the __block keyword to the local variable and the local variable can be modified inside the block

Defining block types with typedef

typedef int (^myblock) (int, int);

You can then use this type of Myblock to define the block variable

Myblock Block;

Myblock B1, B2;

bloc s with no return value, no formal parameters

Block with a return value and a tangible parameter

Here is a copy, a reference to the blog

Second, memory location (arc condition)

1, block block storage location (block block entry address): May be stored in 2 places: Code area, heap area (the program is divided into 5 areas, there is a constant area, the global zone and the stack area, for the MRC case code may also exist in the stack area. For more information about partitioning: http://www.cnblogs.com/mddblog/p/4405165.html

Scenario 1: Code Area

Variables that are in the stack (such as local variables) are not accessed, and variables that are in the heap (for example, objects created by alloc) are not accessed. This means that accessing global variables is also possible.

/**  does not have access to any variable */int main (int argc, char * argv[]) {    void (^block) (void) = ^{        NSLog (@ "= = =");    };    Block ();}

/**  accesses the global (static) variable */int  IVar = 10;int main (int argc, char * argv[]) {    void (^block) (void) = ^{        NSLog (@ "= = = = %d ", iVar);    };    Block ();}

Scenario 2: Heap Area

If you access a variable in the stack (for example, a local variable) or a variable that is in the heap (for example, an object created by Alloc). will be stored in the heap area. (It is actually placed in the stack area, and then the arc is automatically copied to the heap area)

/**  Access local variable */int main (int argc, char * argv[]) {    int iVar = ten;    void (^block) (void) = ^{        NSLog (@ "===%d", IVar);    };    Block ();}

2. Precautions

When the code is stored in a heap area, you need to pay special attention because the heap area is not changing as the code area does, and the heap area is constantly changing (destroying is constantly created). Therefore, the code may be destroyed (when there is no strong pointer pointing), and if this code is accessed again then the program crashes. Therefore, for this case, we should specify a block property as strong, or copy:

@property (nonatomic, strong) void (^myblock) (void); So there's a strong pointer pointing to it.

@property (nonatomic, copy) void (^myblock) (void); Does not copy a copy in the heap area for three reasons

In the case of the 1th scenario (where the code has a code area), it is possible to use strong,copy (which does not replicate a copy to the heap area). Therefore, it is best to specify strong (recommended) or copy when defining a block.

3. Will a copy be copied after the specified copy? (or a shallow copy or a deep copy).

1 copy mutable variable: The memory area pointed to by the pointer is also copied while the pointer is being assigned. Deep copies, such as Nsmutablestring objects.

2 Copy immutable variable: equivalent to strong, or a shallow copy, such as a NSString object.

Because block is a piece of code that is immutable, it is not deeply copied.

Objective-c (use of block)

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.