Block and GCD is a must-ask question for iOS senior programmers, this chapter introduces the next block
The first part: overview
A block is a C-level syntax and a feature of the runtime, similar to a function (function pointer) in standard C, but its operation requires compiler and runtime support. From the beginning of the ios4.0 very good support block, personal feel the use of block the most convenient is a simplified callback process, previously used UIView animation, the process to control the animation after the corresponding processing, ios4.0, UIView new block support, now as long as the use of a simple blo CK code can be written in the code part of the animation to add directly after the end of the operation, there is the use of notification when the block is also very helpful, anyway, more than the right, block greatly improve the efficiency of the Code,
block is an inline function with anonymous functionality, very similar to the function,but the block is more flexible than the C function, which is represented by a reference to stack memory, heap memory, and we can even pass a block as a parameter to another function or block.
The Entity form is as follows:
^ (Incoming parameter column ) { behavior body }
lock entity begins with" ^ ", followed by a parameter column (e.g. int A, int b, int c), behavior principal Enclosed by curly braces, the exclusive name is block literal. behavior body can be returned with return value, the type will be automatically identified by compiler. If there is no to be written: ^ (void).
Part II: Specific usage
One: primary usage of block
/*test 1 uses the ^ operator to declare a block variable */int multiplier = 7; Int (^myblock) (int) = ^ (int num) {return num*multiplier; }; printf ("%d\n", Myblock (5)); /*test 2 Writes the contents of the block directly in an inline way where block is needed */char * mycharacters[3] = {"Tomjohn", "George", "Charles Condomine"}; Qsort_b (mycharacters, 3, sizeof (char*), ^ (const void*l,const Void*r)//Sort function, personal feeling sort quick sort is good to use { Char*left = * (char**) L; char*right= * (char**) R; Return strncmp (left, right, 1); } ); printf ("%s\n", Mycharacters[0]); /*test 3 Modify the variable within the block */__block int multiplier2 = 7; Int (^MYBLOCK2) (int) = ^ (int num) {if (num>5) {multiplier2 = 0; }else{multiplier2 = 3; } return multiplier2; }; NSLog (@ "%d,%d\n", Myblock2 (8), Myblock2 (3));
Two: Define a block
Reference to the entity form of block
/* return void, parameter is void block*/ void (^blockreturningvoidwithvoidargument) (void); /* Callback integer, two parameters are integer and character form block*/ int (^blockreturningintwithintandchararguments) (Int,char); /* return void, array with 10 blocks, each with an integer-shape parameter */ void (^arrayoftenblockreturningvoidwinintargument[10]) (int);
Three: block and variable
/*test 1 How the zone variable is used * ///plus __block modifier to modify operation int x = 123 in block; void (^printxandy) (int) = ^ (int y) { printf ("%d,%d\n", X, y); }; Printxandy (a); Add the __block modifier to manipulate the /*test 2 Interaction between the various types of variables and blocks in block */ nsinteger localcounter =; __block Char Localcharacter; void (^ablock) (void) = ^ (void) {// ++counterglobal;//can be accessed ++counterstatic;//can be accessed// Counterglobal = Localcounter; Localcharacter = ' a '; }; ++localcounter; Localcharacter = ' B '; Ablock (); printf ("test2:%ld,%ld,%c\n", Localcounter,counterstatic,localcharacter);
Four: Using block
/*test 1 when a block is declared as a variable, we can use it as a general function */INT (^twofrom) (int) = ^ (int anint) {return anInt-1 ; }; printf ("1 from Ten is%d\n", Twofrom (10)); Float (^distancetraveled) (float,float,float) = ^ (float startingspeed,float acceleration,float time) {Float dist ance = (startingspeed*time) + (0.5*acceleration*time*time); return distance; }; printf ("Howfar%f\n", distancetraveled (0.0,9.8,1.0)); /*test 2 in general, if you consider block as a parameter pass-through function, we will usually use the inline method to use block*/char*mycharacter_s[3] = {"Tomjohn", "George", "Charle S Condomine "}; Qsort_b (mycharacter_s, 3, sizeof (char*), ^ (const void*l,const void*r) {char*left = * (char**) L; Char*right = * (char**) R; Return strncmp (left, right, 1); }); /*test 3 In the example above, the block itself is part of the function parameter, and in the next example the Dispatch--apply function uses the block,dispatch_apply definition as follows *///void Dispatch_apply (si ze_t Iterations,dispatch_queue_t queue,void (^block) (size_t)); size_t count = 12; dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); Dispatch_apply (count, queue, ^ (size_t i) {//similar to For loop printf ("%zu\n", i*2); }); The/*test 4 SDK provides a lot of ways to use block, we can pass the block as a general parameter, the following example shows how to get the index value of the data we want in the first wubi of an array *//all the data nsarray*a Rray = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", @ "a", @ "B", @ "Z", @ "G", @ "is", @ "Q", nil]; We just need the information in this set nsset*filterset = [Nsset setwithobjects:@ "A", @ "B", @ "Z", @ "Q", nil]; BOOL (^test) (id obj,nsuinteger idx,bool*stop); Test = ^ (id obj,nsuinteger idx,bool *stop) {if (idx<5) {if ([Filterset containsobject:obj]) {return YES; }} return NO; }; nsindexset*indexes = [array indexesofobjectspassingtest:test]; NSLog (@ "indexes:%@", indexes);
If the similarities are purely coincidental,
iOS Block primary use