IOS learning 18: block programming for learning details

Source: Internet
Author: User

This is a detailed tutorial on iOS block.
I have searched official documents and can understand them in poor English. Slow speed. Later, I found the translation of most of the above documents and thought it was quite good. For block learning. Roughly divided into several points 1. SDK restrictions, that is, the version of the block to which the new feature is introduced. 2. Basic syntax 3. Scenario use. I. Block description

Block is a new program syntax added after ios sdk 4.0. Strictly speaking, the concept of block is not within the scope of basic programming. It is not easy for beginners to understand, but after ios sdk 4.0, block appears in almost all new versions of APIs. In other words, if you do not understand the concept of block, you cannot use new functions after SDK 4.0. Therefore, although the block syntax is a bit difficult, however, in order to use the new features of iOS, we still have to learn about this new concept.

Basically, I explained the reason why block was introduced after 4.0 and why we want to learn it ~

Ii. Basic syntax

Block can be regarded as a statement block or often used as an anonymous function.

 (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL

[ UIView animateWithDuration:10 animations:^{       NSLog(@"%@,",@"block");    }];

Let's simply look at this function.

The animations parameter accepts a block function block. We can write this block directly by ^.

Then, let's look at the above function declaration. We can see that the parameters accepted by animations are as stated (void (^) (void) animations;

We know that all parameters require a declarative type, for example

(Nstimeinterval) duration. (Void (^) (void) is actually a statement of a common format of block. It also needs to be divided:

The first void indicates that the block function does not return any value, that is, the {} execution does not return any value. (^) Is a fixed specification, indicating that this is a block type. The following () indicates that the block accepts parameters.

Of course, here is (void) We know, does not accept the parameter.

Paste an official figure here

This figure contains the Declaration and definition.

In fact, we need to know three things.

1. How to declare a Block Function

INT (^ testblock) (int A, int B );

Return Value Type (^ block name) (input parameter)

2. OC declares a function with block Parameters

-(Void) textf :( bool (^) (ID a, Id B) bblock;

As we can see above, this is basically similar to the statement, that is, the name is switched out.

(Return type (^) (input parameter) block parameter name

Then, when the C code is used to declare the method:

Void
Dispatch_apply (size_t iterations, dispatch_queue_t queue, void (^ block) (size_t ));

3. Implement a block function block after declaration

^ (Int A, int B ){

// Code;

}

The basic block syntax is as follows.

Several important points when using block:

1. objects outside the block function only have the read permission in the block statement block.

2. The _ BLOCK statement on the External Object solves the problem 1.

3. Interaction between variables and blocks:
 

13: extern nsinteger counterglobal; 14: static nsinteger counterstatic; 15: {16: nsinteger localcounter = 42; 17: _ block char localcharacter; 18: void (^ ablock) (void) = ^ (void) 19: {20: ++ counterglobal; // accessible. 21: ++ counterstatic; // accessible. 22: counterglobal = localcounter; // localcounter cannot be changed when the block is created. 23: localcharacter = 'a'; // set the localcharacter variable defined outside. 24 :}; 25: ++ localcounter; // the value in the block that will not be affected. 26: localcharacter = 'B'; 27: ablock (); // executes the block content. 28: // after execution, localcharachter will change to 'A' 29 :}

This example is easy to understand.

5. Counter + 1 for instance variables referenced in BLOCK statement blocks and troubleshooting methods. This is also a point of attention, as well as some problems that may occur in the cold questions.

6. Because the SDK is restricted, you may need to use the new feature block in the code version to support the previous system.

Ns_blocks_available for judgment and code adaptation.

7. Personally, block is used as an advanced level in code or programming. It may be rarely used because of its habits. I even asked a friend to say that I have never heard of it ~

However, as an alternative to proxy or callback, it can be gradually used and accepted.

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.