first, what is blocks
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, and is well-supported from ios4.0.
second, in iOS development, under what circumstances use block
In addition to being able to define parameter lists, return types, blocks can also obtain state (such as local variables) within the lexical scope that is defined, and can modify these states under certain conditions, such as using the __block variable. In addition, these modifiable states are shared among multiple blocks within the same lexical scope, and can continue to share or modify these states, even if the lexical scope (such as stack expansion, out of scope) is out. In general, blocks are encapsulated in short snippets of code that are used as work units, often for concurrent tasks, traversal, and callbacks.
third, how to declare the block (in contrast to the C language function declaration)
Four, C function and blocks call
Int (*cfunc) (int a) function call
int result = Cfunc (10);
Int (^bfunc) (int a) function call
int result = Bfunc (10);
Five, __block key words
The inside of a block can refer to its own extraterritorial variables, including static variables, extern variables or free variables (when defining a variable, if no modifier is stored, by default the free variable Auto,auto variable is saved in the stack. In addition to auto, there are storage modifiers such as register,static, which are read-only in the block for free variables. In addition to the introduction of block, a special __block keyword variable storage modifier is introduced.
1#import"ViewController.h"2 3 @interface Viewcontroller ()4 5 @end6 7 @implementation Viewcontroller8 9- (void) ViewdidloadTen { One [Super Viewdidload]; A //additional setup after loading the view, typically from a nib. - - intresult=^ (intA) {return(a+a-a*a);} (5); theNSLog (@"result:%d", result); - - -NSString * (^ADDSTR) (nsstring*,nsstring*); +addstr=^ (NSString *str1,nsstring *str2) { - +NSString *str=[nsstring stringWithFormat:@"%@_%@", STR1,STR2]; A returnstr; at }; - -NSString *str3=addstr (@"Fewfwe",@"Fewefwfrgrthrth"); -NSLog (@"str3:%@", STR3); - - intD=5; in int(^addnum) (intAintb); -addnum=^ (intAintb) { to + intc=a+d-b; - returnC; the }; * intE=addnum (2,6); $NSLog (@"c=%d", e);Panax Notoginseng -D=Ten; the intF=addnum (2,6); +NSLog (@"f=%d", f); A theNSString *strr1=@"sssss"; +NSString *rstr1=addstr (STRR1,@"222"); -NSLog (@"rstr:%@", RSTR1); $ $NSString *strr2=@"KKKKK"; -NSString *rstr2=addstr (STRR2,@"222"); -NSLog (@"rstr2:%@", RSTR2); the - }Wuyi the -- (void) didreceivememorywarning Wu { - [Super didreceivememorywarning]; About //Dispose of any resources the can be recreated. $ } - -@end
iOS development Block (a) code block