On the block of iOS learning

Source: Internet
Author: User

1. Block Concept

Block is an extension of the C language introduced by ios4.0+ and Mac OsX 10.6 to implement the features of anonymous functions. The so-called anonymous function, also called the closure function, allows you to create a temporary function that does not have a specified name. The value most often used as a callback function (callback) parameter, and of course there are other uses. For example, as a variable value to use, the specific usage will be described later.

2. Block definition:in iOS, the caret "^" is used to declare a block variable, and the contents of the block are contained in "{}" and are used as the C language ";"to indicate the end of the statement. The specific definition is as follows:

1? We'll start by defining a simple, no-return-value, parameterless block:
-(void) blocktestone{    void (^blockone) (void) = ^ (void) {        NSLog (@ "This is Blockone");    };    Blockone ();}
As you can see, the above code, we define a no return value in a function Blocktestone, no parameter block, called Blockone, and then we call this blockone later. Because our block has no parameters, it can also be written in simple writing:
-(void) blocktestone{    void (^blockone) (void) = ^{        NSLog (@ "This is Blockone");    };    Blockone ();}
That is, the argument list after the equals sign is omitted.Console Output results:


2?? Next, we define a block with parameters and no return value:
-(void) blocktesttwo{    void (^blocktwo) (int) = ^ (int a) {        NSLog (@ "Blocktwo = =%d", a);    };    Blocktwo (20);}
Here we define a block variable with no return value, type int, named Blocktwo, then we call Blocktwo, pass in Parameter 20, the console prints the result:


3?? Then we'll define a block with a return value and a parameter:

-(void) blocktestthree{    int (^blockthree) (int) = ^ (int b) {        NSLog (@ "Blockthree parameter =%d", b);        return ten;    };    NSLog (@ "%d", Blockthree ());
Here we define a block variable with a return value of int, and a parameter of type int, and then the block implementation prints his parameters, and finally returns a constant of 10; we also printed the Blockthree in block and gave him a parameter of 20. The console prints the result as:


It is important to note here that, because the blockthree we define is a return value, when in his implementation (that is, the equal-right curly brace), if there is no return value, the compiler will give us an error directly:



3. The storage domain of the block. you've seen me. Blog post "block pass value and use block to encapsulate a network request class" Click to open the link friend must be curious, why use block defined properties, to use the copy feature?
Here we analyze the storage domain of the block, and we all know it. Look at the code first:
-(void) testblock{    void (^blockone) (void) = ^{        NSLog (@ "This is Blockone");    };    int c = Ten;    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 have defined three block variables, Blockone,blocktwo and Blockthree, and then we print their three addresses, three of them, the difference is that Blockone realized just print a word, Instead of using any external variables (variables other than block definitions), Blocktwo implements a print of a word, writes an external variable C, and Blockthree implements a copy of the Blocktwo, and then the console prints the following result:



It is strange to see that these three blocks belong to three memory areas, blockone because no external variables are used, his storage area in the global zone, and blocktwo because of the use of external variables, then suddenly ran to the stack area as the stack area, and blockthree because copy of Blocktwo, then changed into heap memory. As we all know, the objects stored in the global zone and the heap are relatively safe, but the objects stored in the stack are relatively dangerous, and it is possible that their objects have been released when using him, resulting in wild pointers, resulting in the crash of the program. So, when we use the BLCOK member variable or property, we want to copy it to the heap memory for use.
not to be continued ... And the use of __block, remember to pay attention to Oh! It's too late today ...

On the block of iOS learning

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.