32-OC Block

Source: Internet
Author: User

Block Basic Concepts

Attention and pointers to functions when learning

What is block

Block is a special type of data used in iOS to save a piece of code

The role of Block

Blocks are used to save a piece of code that can be removed at the appropriate time to invoke functions similar to functions and methods

Format of the problem 3:block

Answer: Block format:

return value type (^block variable name) (parameter list) = ^ (parameter list) {

};

Block and typedef

How do I use Tydedef to alias a block? Note comparison with pointers to functions

Use a typedef to alias a block, like a pointer to a function, the name of a block variable is an alias

Block Application Scenario

What is the application scenario?

When you find that the front and back of the code are the same, you can use block at this time

How to access and modify values outside the block

The outside variables can be accessed in 1.block

int a = 10;

void (^myblock) () = ^{

NSLog (@ "a =%i", a);

};

Myblock ();

Why can't I modify the values of external variables in block

By default, you cannot modify the value of an external variable in a block.

Because the variables in block and outside variables are not the same variable

If an external variable is accessed in the block, the block copies the external variables into the heap memory

Because the external variables used in the block are copy, modifying the values of the external variables before the call does not affect the copy value in the Block

int a = 10;

NSLog (@ "&a =%p", &a);

void (^myblock) () = ^{

A = 50;

NSLog (@ "&a =%p", &a);

NSLog (@ "a =%i", a);

};

A = 20;

Myblock ();

__block's application Scenario

If you want to modify the value of an external variable in a block, you must precede the external variable with __block

Why not add __block the value of external variables cannot be modified in block

Add __block After the address is passed, so you can modify the value of the external variables in the block

Whether the block is stored in a heap or in a stack

Block is stored in the stack by default, and if a copy operation is made to the block, the block is transferred to the heap

If block is in the stack and the object is accessed from the block, then the object is not retain.

But if the block is in the heap and the block accesses the outside object, then the outside object will be retain once.

How to manage the block memory

If the object in the block is accessed, be sure to add block to the object, as long as the block, even if the block is in the heap, does not retain the outside object.

If it's in ARC development, you need to add __weak to the front.

__block person *p = [[Person alloc] init];

How to avoid a circular reference to a block

1. Save the object with a value pointer decorated with __weak 2. Or use Block_copy (Myblock)

If it's in ARC development, you need to add __weak to the front.

32-OC 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.