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
- ^void (ID testobj) {
- testobj= [[NSString alloc] initwithstring:@ "This is a IOS Block test!"];
- NSLog (@ "%@", testobj);
- }
(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
- int testint = ^int {
- return7;
- };
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
- Declaring a block type variable
- Int (^TESTBLK) (int);
- Declares a block type variable with the syntax "return value type (^block code block Name)" (Parameter Type list) ""
- Assigning block code blocks to block type variables
- Testblk = ^ (int count) {
- Returncount + 1;
- };
(2) in block syntax, block code blocks can be used as parameters for function transfer.
For example:
[OBJC]View PlainCopy
- void TestFunction (Bool testbool, Int (^testblk) (int)) {
- if (testbool) {
- TESTBLK;
- }
- }
(3) in block syntax, block blocks of code can be used as return values for functions.
For example:
[OBJC]View PlainCopy
- Int (^TESTBLK) (int) testfunction () {
- return^ (int count) { return count + 1;};
- }
(4) declaring block type with typedef is advantageous for code readability.
For example:
[OBJC]View PlainCopy
- Declares the block type, as the parameter type of the block
- typedef INT (^testblocktype) (int);
- int testint = 0;
- When passed as a parameter
- void TestFunction (Testblocktype testblk) {
- testblk= ^ (int count) { return count + 1;};
- Testint= testblk (10);
- }
- When you return a value
- Testblocktype TestFunction () {
- return (^ (int count) { return count + testint;});
- }
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
- int testint = 0;
- void (^BLK) () = ^{printf ("value =%d", Testint)};
- Blk ();
- The result is printed here: Value = 0
- Testint = 1;
- Blk ();
- 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
- int testint = 0;
- void (^BLK) () = ^{Testint = 1;};
- Blk ();
- 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
- __block int testint = 0;
- void (^BLK) () = ^{Testint = 1;};
- 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