Block of Objective-c

Source: Internet
Author: User
Tags vars

 block of Objective-cTags: iosobjective-cos xblock2016-04-13 14:05 226 people read Comments (0) favorite reports Classification:Small white iOS notes (3)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Directory (?) [+]

Statement:

1, if the article content related to other has been published, but the article did not mention the matter reproduced, please contact me in time.

2, this article for personal understanding, if some of the knowledge points and the actual situation discrepancies, please ignore this article.

3. If this document explains any problems that result in your loss, you are not responsible.

4, this article is written to the small white, the great God can ignore, or raise some views, but ... Don't spray (or tap).

Body: 1 Understanding Block1.1 Concepts

(1) An anonymous function (code block) with local variables.

(2) Using blocks can not declare C + + and Objective-c classes, and do not use static variables, static global variables, or global variables, only use the source code of the C language function to write the use of anonymous functions with local variables.

1.2 Syntax

(1) ^ "Return value type" "(parameter list)" Expression

* Contents in "" "are omitted.

(2) Where the return value type, parameter list can be omitted. Even if there is a return value, the return value type can be omitted, and the compiler will automatically recognize the return value type (nsobject or void).

For example:

[OBJC]View PlainCopy 
    1. ^void (ID testobj) {
    2. testobj= [[NSString alloc] initwithstring:@ "This is a IOS Block test!"];
    3. NSLog (@ "%@", testobj);
    4. }
(3) To create a block, in fact, the system automatically created a function, and the value of the block is actually the function of the pointer to store, pass, similar to the function pointer assignment (block assignment, described below).

2 usage of block 2.1 block type variable

Block syntax allows you to assign block code blocks to variables of block type. That is, the entire block code block can be assigned as a value, and the assignment object is a variable declared as a block type.

2.1.1 As an assignment object with the return value of the block

This type is familiar, however, similar to a normal function, the value of return in a function is used as an assignment object.

For example:

[OBJC]View PlainCopy 
    1. int testint = ^int {
    2. return7;
    3. };

2.1.2 With block as an assignment object

(1) Under block syntax, block code blocks can be assigned to variables declared as block types.

For example:

[OBJC]View PlainCopy 
    1. Declaring a block type variable
    2. Int (^TESTBLK) (int);
    3. Declares a block type variable with the syntax "return value type (^block code block Name)" (Parameter Type list) ""
    4. Assigning block code blocks to block type variables
    5. Testblk = ^ (int count) {
    6. Returncount + 1;
    7. };

(2) in block syntax, block code blocks can be used as parameters for function transfer.

For example:

[OBJC]View PlainCopy 
    1. void TestFunction (Bool testbool, Int (^testblk) (int)) {
    2. if (testbool) {
    3. TESTBLK;
    4. }
    5. }

(3) in block syntax, block blocks of code can be used as return values for functions.

For example:

[OBJC]View PlainCopy 
    1. Int (^TESTBLK) (int) testfunction () {
    2. return^ (int count) { return count + 1;};
    3. }

(4) declaring block type with typedef is advantageous for code readability.

For example:

[OBJC]View PlainCopy 
  1. Declares the block type, as the parameter type of the block
  2. typedef INT (^testblocktype) (int);
  3. int testint = 0;
  4. When passed as a parameter
  5. void TestFunction (Testblocktype testblk) {
  6. testblk= ^ (int count) { return count + 1;};
  7. Testint= testblk (10);
  8. }
  9. When you return a value
  10. Testblocktype TestFunction () {
  11. return (^ (int count) { return count + testint;});
  12. }

2.2 In block, intercept automatic variables

In block blocks of code, you intercept variables outside of the block, and the values and usage of the variables are subject to the rules of block syntax.

2.2.1 Intercept automatic variables

(1) block, block code block intercept automatic variable value, is saved in the instantaneous value of the automatic variable.

For example:

[OBJC]View PlainCopy 
    1. int testint = 0;
    2. void (^BLK) () = ^{printf ("value =%d", Testint)};
    3. Blk ();
    4. The result is printed here: Value = 0
    5. Testint = 1;
    6. Blk ();
    7. The result is printed here: Value = 0

(2) in block, after intercepting an automatic variable, the value of the variable cannot be overwritten.

For example:

[OBJC]View PlainCopy 
    1. int testint = 0;
    2. void (^BLK) () = ^{Testint = 1;};
    3. Blk ();
    4. Error here. In block code blocks, the value of an automatic variable that is not inside the block cannot be overwritten.

2.2.2 __block specifier

If you need to overwrite the value of an automatic variable outside the block in the block code, you need to append the __block specifier to the automatic variable.

For example:

[OBJC]View PlainCopy 
    1. __block int testint = 0;
    2. void (^BLK) () = ^{Testint = 1;};
    3. Blk ();

Reference:

Objective-c Advanced Programming iOS and OS X multithreading and Memory Management "Day" Kazukisakamoto Tomohiko furumoto Tony Lai Translation

Block of Objective-c

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.