Lattice 15: The Block I understand (1)

Source: Internet
Author: User

1, Block is essentially a struct structure, in this structure, the most important member is a function (of course, in addition to functions and other important members).

2, before starting to parse block, first to review the format of block. There are 2 block-related formats:

(1), block object format;

(2), block variable format;

3. For block objects, it has several common formats:

(1), a complete block object is in the following format:

For example, this block object:

You can see that the definition of a complete block object and function is very similar, such as this function:

The difference between the two is that theBlock object has more than one "^" symbol, and does not need to specify the function name as functions;

(2), in the full format of some items can be omitted, such as "return value type". This item can be omitted regardless of whether the block object has a return value, omitted, as long as the expression has a return statement that automatically uses the type of the return value, and if there is no return statement, the void type is used.

In this case, the format of the Block object becomes:

So the Block object in (1) above can be written like this:

(3), in addition, if the block object does not require parameters, then the "parameter list" This item can also be omitted, the format of the Block object becomes:

For example, a block object like this:

4. After defining the Block object, you also define the block variable to hold it to make it easier to use the Block object. Review the function of 3 (1):

For this function, you can declare a function pointer type variable to use it:

In this case, using the *funcptr is equivalent to using the Func function.

Similarly, for a block object in 3 (1), you can also declare a block variable:

Using block () at this time is equivalent to using the Block object.

Comparing the declaration of the function pointer and the block variable, you can see that the two are very similar, except that the * number is changed to the ^ number only .

5. If you want to pass a block variable as a parameter, it is obviously a hassle to define it completely each time it is used. For example, to use the parameters of the block variable type in a function, the complete definition should look like this:

This is obviously cumbersome, so you can simplify the variables by typedef:

Then this function can be defined as:

The block variable can then be used very conveniently.

6, review the block related format, and then start to discuss the implementation of block.

As mentioned earlier, the most important component of block is a function, which means that most of the functions that block can implement are actually implemented using functions. Let's try it out in the next step. How to implement block functionality without using block.

Consider the question: Suppose there are two buttons button1 and button2, which require the following functions:

(1), the button is numbered;

(2), the number can be modified;

(3), click the button to output their own number.

7, the first simple method can be achieved by using two functions:

Two functions corresponding to two button clicks, call the function when the number of the button as a parameter passed in, you can print out the corresponding number.

It seems to be satisfying, but the problem with this approach is obvious: the button does not hold its own number, and it needs to call the function to pass the number in.

This approach is clearly not perfect.

8, according to object-oriented thinking, by abstracting the button into a class and defining the numbering as an instance variable of the button class, this function can be realized. But here just want to use the ordinary C language to implement, then you can do so, the number of each button is defined as a global variable to save, the modified code is as follows:

This allows the button to "hold" the number, achieving the 3 requirements of the problem.

9, back to look at the problem, if you want to use block to achieve these 3 requirements, the code will be like this:

The functionality implemented by this code is exactly the same as what 8 of the code does.

10, in fact, these two pieces of code is almost equivalent, 8 of the code is almost 9 of the code into the normal C language after the appearance (note "Almost" the word, in fact, 5 of the code into the normal C language and 4 code is still a lot of difference).

The 3 requirements that are mentioned in the question are actually the 3 main function points of blocks:

(1), can intercept variables;

(2), can intercept variable variables;

(3), callback.

Lattice 15: The Block I understand (1)

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.