How block is used in iOS

Source: Internet
Author: User

X.1 on block

In this section we first use some simple examples to import the concept of block.

x.1.1 declaring and using block

We use the "^" operator to declare a block variable, and add ";" at the end of the block definition. To represent a complete statement (that is, to treat the entire block definition as a simple statement as described in the previous section, since the entire definition must be a complete sentence, and therefore must be preceded by a semicolon), here is an example of a block:

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

We use it to explain this example (please translate the words in the text box as follows):

We declare a "myblock" variable and use the "^" symbol to indicate that it is a block.

This is the complete definition of block, which will be assigned to the "myblock" variable.

Indicates that "myblock" is a block with a callback value of integer (int).

It has a parameter, and the pattern is also an integer.

The name of this parameter is called "num".

This is the contents of the block.

It is important to note that blocks can use the same variables that define the scope themselves, and you can imagine that in the example above, multiplier and myblock are all two variables defined within a function, and this variable is in the middle of a function two braces "{" and "}". Because their effective range is the same, the multiplier variable can be used directly in the block, and when the block is defined as a variable, we can use it directly as a general function:

   1:int multiplier = 7;
   2:int (^myblock) (int) = ^ (int num)
   3: {
   4:     return num * multiplier;
   5:};
   6:printf ("%d", Myblock (3));
   7://Results will print out 21

x.1.2 Direct use of block

In many cases, we do not need to declare the block as a variable, whereas we can directly write the block's contents directly in the place where we need to use block, in the following example Qsort_b function, which is a similar traditional qsort_t function, But use block directly as its parameter:

   1:char *mycharacters[3] = {"Tomjohn", "George", "Charles Condomine"};
   2:qsort_b (Mycharacters, 3,
   3:          sizeof (char *),
   4:          ^ (const void *l, const void *r)//block part
   5:             {
   6:                 Char *left = * (char * *) L;
   7:                 Char *right = * (char * *) R;
   8:                 return strncmp (left, right, 1);
   9:             }                            //end
  10:);

x.1.3 __block Variable

In general, only variables in the same scope can be read within the block and there is no way to modify any variables defined outside the block, and if we want these variables to be modified in the block, we must hang the __block modifier in front of it, in the first example above Multiplier, this variable in the block is read-only, so multiplier = 7 after the specified, in the block multiplier can only be 7 can not be modified, if we modify the multiplier in the block, the editor will Generates an error, so if you want to modify multiplier in a block, you must precede the multiplier with the __block modifier, see the following example:

   1: __block int multiplier = 7;
   2:int (^myblock) (int) = ^ (int num)
   3: {
   4:if (num > 5)
   5: {
   6:     multiplier = 7;
   7:     }
   8:     Else
   9:     {
  Ten:         multiplier = 10;
  One:     }
  :     return num * multiplier;
  13:};

Example code for using block

- (void) viewdidload{[Super Viewdidload]; //Do any additional setup after loading the view.UIView*view = [[UIView alloc] Initwithframe:cgrectmake (0,0, -, $)]; View.backgroundcolor=[Uicolor Yellowcolor];    [Self.view Addsubview:view]; void(^blockanimation) () = ^() {view.frame= CGRectMake ( -, -, -, $);    }; void(^blockfinished) (BOOL finished) = ^(BOOL finished) {View.frame= CGRectMake (0,0, -, $);    }; [UIView animatewithduration:1.0animations:blockanimation completion:blockfinished]; }

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.