Introduction to block in iOS

Source: Internet
Author: User

I. Overview

Block is a C-level syntax and runtime feature. Block is similar to the C function, but the block is more flexible than the C function, which is reflected in the stack memory, heap memory reference, and we can even pass a block as a parameter to other functions or block.

Second, warm up

Let's look at a relatively simple block example:

    1. int multiplier = 7;
    2. Int (^myblock) (int) = ^ (int num) {
    3. return num * multiplier;
    4. };

In this example, Myblock is a block variable that takes a parameter of type int and returns a value of type int. Does it look like a C function?

Come on, let's have a typedef.

    1. typedef VOID (^boolblock) (BOOL); A block that accepts only one bool parameter and no return value
    2. typedef INT (^intblock) (void); A block with no parameters that returns an int
    3. typedef boolblock (^hugeblock) (Intblock); //See, this hugeblock parameter and return value are block

Third, more detailed examples

Note: The typedef above are also valid ~

Take the initiative to call:

    1. -(void) SomeMethod
    2. {
    3. Boolblock Ablock = ^ (BOOL bValue) {
    4. NSLog (@"Bool block!");
    5. };
    6. Ablock ();
    7. }

Returned as a parameter:

    1. typedef VOID (^boolblock) (BOOL);
    2. -(Boolblock) foo ()
    3. {
    4. Boolblock Ablock = ^ (BOOL bValue) {
    5. NSLog (@"Bool block!");
    6. };
    7. return [[ablock copy] autorelease]; //must copy, copy it to the heap, more detailed principles, will be explained in the following chapters
    8. }

A member of the class:

    1. @interface Obj1:nsobject
    2. @property (nonatomic, copy) Boolblock block; //Reason ditto, students
    3. @end
    4. OBJ1 *obj1 = ...
    5. Obj1.block = ^ (BOOL bValue) {
    6. NSLog (@"Bool block!");
    7. };

Parameters for other functions:

    1. -(void) foo (Boolblock block)
    2. {
    3. if (block) {
    4. Block ();
    5. }
    6. }

Even other block parameters:

    1. Boolblock bblock = ^ (BOOL BV) {if (BV) {/*do some thing*/}};
    2. Hugeblock Hblock = ^ (boolblock bb) {BB ();};
    3. Hbolck (bblock);

Ah, global Variables! :

    1. static int (^maxintblock) (int, int) = ^ (int A, int b) {return a>b?a:b;};
    2. int main ()
    3. {
    4. printf ("%d\n", Maxintblock (2,10));
    5. return 0;
    6. }

Well, you know how the block might be used.

Four, special markings, __block

If you want to modify the stack variables declared outside of block within a block, be sure to add __block tags to the variable:

    1. int main ()
    2. {
    3. __block int i = 1024x768;
    4. Boolblock bblock = ^ (BOOL BV) {
    5. if (BV) {
    6. i++; //If there is no __block tag, it cannot be compiled.
    7. }
    8. };
    9. }

Article Source: http://www.cnblogs.com/shouce/p/5522180.html

Introduction to block in iOS

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.