The value of iOS transfer values (i)

Source: Internet
Author: User
the value of iOS transfer values (i)

  ios4.0 System has started to support block, in the programming process, blocks is obj-c as an object, it encapsulates a piece of code, this code can be executed at any time. Blocks can be the return value of a function argument or function, and can itself take an input parameter or return value. It is similar to a traditional function pointer, but there is a difference: blocks is inline, and it is read-only to local variables.

Here is the theoretical part:

1, the definition of block

1//Declaration and implementation written together, like the declaration of a variable to implement int a = ten;
 2        Int (^ablock) (int, int) = ^ (int num1, int num2) {
 3  
 4 return    num1 * NUM2;
 5  
 6      };
 7//Declaration and implementation of separate, just like the variable first declared after the implementation of int a;a =;
 8         Int (^cblock) (int,int);
 9         cblock = ^ (int num1,int num2)
{One return             num1 * NUM2;         ;  

It defines a blocks object named Ablock and carries relevant information:

1, Ablock has two form parameters, respectively, int type;

2, the return value of ablock is int type;

3, the right side of the equation is the concrete realization of blocks;

4, ^ with side blocks declaration and implementation of the logo (keywords);

Of course, you can define other forms of block. E.g: no return value, no formal parameters, etc.

1         void (^bblock) = ^ ()
2         {
3             int a = ten;
4             printf ("num =%d", a);
5         };    

2, Blocks access rights

Blocks can access local variables, but cannot be modified.

1         int a = ten;
2         Int (^dblock) (int) = ^ (int num)
3         {
4             a++;//not work.
5 return             num * A;
6         };

The reason why this cannot be modified here is that it is determined at compile time to copy the value of a to block as a new variable (assuming a ' = 10), at which point a ' and a ' are not related.

This is where the value in the function is passed. Add keywords If you want to modify: __block or static

1         __block int a = 7;
2         Int (^dblock) (int) = ^ (int num)
3         {
4             a++;//work.
5 return             num * A;
6         };

3, Block's call

Block calls are like calling functions. e.g:

1 int c = Ablock (10,10); <br> bblock ();

4. Block application

If we are familiar with proxy recursion, we may love and hate the agent. We first build the Model a page Push b page, if the value of page A is passed to the B page, attributes and a single example of the value can be done. But if the value of the B page is passed to page A in the pop process, it can be either a single example or an agent. When it comes to proxies, we have to declare a protocol and create a proxy, which is troublesome. Often we pass a number that requires a lot of code to be written between two pages, which changes the overall order of the page and discounts the readability. So, at this point, block is an optimization scheme. Everyone tired, rest, foresee how the funeral, listen to let's ...

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.