Introduction to iOS Block and iosblock

Source: Internet
Author: User

Introduction to iOS Block and iosblock

A Block is an object that encapsulates a piece of code that can be executed at any time. A block can be used as a function parameter or return value, and it can contain an input parameter or return value. It is similar to the traditional function pointer, but there is a difference: the block is inline and it is read-only for local variables.

1. Entity form

The actual behavior of a Block is similar to that of a Function. The Block Entity form is as follows:

^ (Input parameter column) {behavior subject };

The Block object starts with "^", followed by the parameter columns wrapped in parentheses (such as int a, int B, int c), and the behavior subject is wrapped in braces, the private name is block literal. The behavior subject can return the value with return, and the type will be automatically identified by compiler. If no parameter Column exists, it must be written as ^ (void ).

Block definition:

Int (^ myBlock) (int a, int B) = ^ (int a, int B ){
Return a + B;
};

In this example, a blocks object named myBlock is defined. It has two int parameters and returns int. The right side of the equation is the specific implementation of blocks. Is it a bit like the definition of a method?

2, Block features

(1) define a Block variable in a class, just like defining a function;

(2) A Block can be defined inside a method or outside a method;

(3) the code in the body of {} is executed only when Block is called;

3. Block syntax

Summary:

(1) block as a local variable (local variable)

ReturnType (^ blockName) (parameterTypes) = ^ returnType (parameters ){...};

(2) block as a member attribute of the class (@ property)

@ Property (nonatomic, copy) returnType (^ blockName) (parameters );

In this case, we can use delegate to implement proxy functions.

(3) block as the function parameter (method parameter)

-(Void) someMethodThatTakesABlock :( returnType (^) (parameterTypes) blockName;

Call a function that includes the block parameter,

[SomeObject somethodThatTakesABlock: ^ returnType (parameters) {...}];

(4) use typedef to define the block type

Typedef returnType (^ TypeName) (parameterTypes );

TypeName blockName = ^ returnType (parameters ){...};

The above content is translated from fuckblocksyntax. You can read it repeatedly when you forget the block syntax.

 

4. Example of Block usage

As a local variable:

Int (^ Mutiply) (int, int) = ^ (int num1, int num2 ){

Return num1 * num2;

};

Block can access local variables, but cannot be modified. For example, the following code will report a compilation error.

Int num = 0;
// Use block
Int (^ myBlock) (int a, int B) = ^ (int a, int B ){
Num = a + B;
Return num;
};

 

If you want to modify it, add the keyword __block (Note: there are two underlines "_")

_ Block int num = 0;
// Use block
Int (^ myBlock) (int a, int B) = ^ (int a, int B ){
Num = a + B;
Return num;
};

_ Block modifies local variables. It calls copy for the block on the stack. Each time a pointer to the block copied to the stack is returned, the _ block variable is copied to the heap.

As a function parameter, blocks replaces the callback function or delegate in a sense. When the function is called, assuming an event is triggered, the content in the block will run. This is conducive to code integration and reading, and you do not need to implement the delegate method everywhere.

 

5. Block advantages

Blocks usually represents a small, self-contained code snippet. Therefore, they are used as encapsulated work units for concurrent execution, or in a set item, or callback when other operations are completed.

Blocks is a practical alternative to traditional callback functions for the following two reasons:

 

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.