Block of ios Learning

Source: Internet
Author: User

Block of ios Learning
1. block Concept

Block is an extension of the C language introduced after ios4.0 + and Mac osX 10.6. It is used to implement the features of anonymous functions. The so-called anonymous function, also known as the closure function, allows you to create a temporary function without a specified name. It is often used as the value of the callback function (callback) parameter. Of course there are other usage. For example, it is used as a variable value. The specific usage will be described later.

2. block definition: in iOS, use the Escape Character "^" to declare a block variable. The content of the block is included in "{}", and "; the end of the statement. The specific definitions are shown in:

1 ?? Let's first define a simple block with no return value and no parameters:
-(void)blockTestOne{    void(^blockOne)(void) = ^(void){        NSLog(@"this is blockOne");    };    blockOne();}
We can see that the above Code defines a block with no return value and no parameter in a function blockTestOne, called blockOne, and then we call this blockOne later. Because our block does not have a parameter, you can also write it as follows:
-(void)blockTestOne{    void(^blockOne)(void) = ^{        NSLog(@"this is blockOne");    };    blockOne();}
The parameter list after the equal sign is omitted. Console output result:


2 ?? Next, we define a block with parameters and no returned values:
-(void)blockTestTwo{    void(^blockTwo)(int) = ^(int a){        NSLog(@"blockTwo == %d", a);    };    blockTwo(20);}
Here, we define a block variable with no return value. The parameter type is int type and its name is blockTwo. Then we call blockTwo and input parameter 20. The console prints the result:


3 ?? Then, let's define a block with a returned value and a parameter:

- (void)blockTestThree{    int (^blockThree)(int) = ^(int b){        NSLog(@"blockThree parameter == %d", b);        return 10;    };    NSLog(@"%d", blockThree(20));}
Here we define a block variable whose return value is int type and whose parameter is int type. Then the block implementation prints its parameters and returns a constant 10; we printed blockThree in the block and passed it a parameter of 20. The result is as follows:


Note that the blockThree we define has a return value. Therefore, if return is not returned in its implementation (that is, in the braces on the right of the equal sign, the compiler will directly report an error to us: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "" alt = "\">


3. block Storage domain. I have read my previous blog post "block pass value and use block to encapsulate a network request class". My friends who click to open a link are curious. Why should I use the copy attribute defined by block to use the copy feature?
Here we will analyze the block Storage domain and you will understand it. First look at a piece of code:
- (void)testBlock{    void(^blockOne)(void) = ^{        NSLog(@"this is blockOne");    };    int c = 10;    void(^blockTwo)(void) = ^(void){        NSLog(@"this is blockTwo %d ", c);    };    void(^blockThree)(void) = [[blockTwo copy] autorelease];    NSLog(@"blockOne address == %@", blockOne);    NSLog(@"blockTwo address == %@", blockTwo);    NSLog(@"blockThree address == %@", blockThree);}
In the above Code, we define three block variables, blockOne, blockTwo, and blockThree. Then, we print the three addresses respectively. The difference between the three of them is, blockOne only prints one sentence without any external variables (variables other than the block definition), while blockTwo prints one sentence and writes an external variable c, what blockThree implements is to copy blockTwo to one point, and then the result printed on the console is as follows:



It is strange to see that the three blocks belong to the three memory regions respectively. Because blockOne does not use any external variables, its storage region is in the global zone, because blockTwo uses external variables, it suddenly runs to the stack zone, that is, the stack zone, and blockThree changes to the heap memory because it copies blockTwo. We all know that objects stored in the global zone and heap are relatively safe, but objects stored in the stack zone are relatively dangerous, it is possible that its object has been released when it is used, resulting in a wild pointer, resulting in crash of the program. Therefore, we need to copy the blcok member variables or attributes to the heap memory.
Not complete... There is also the use of _ block. Remember to pay attention to it! It's too late today...

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.