We can use block as a closure function that accesses external variables and local variables, but by default it is not possible to modify external variables.
You can use it to do callback methods that are more intuitive than using proxies (Delegate).
First, the basic definition of block
Block's basic notation (also detailed):
ReturnType (^blockname) (params) = ^returntype (params) { //};
return type (^block's name) (parameter of block) = ^ return type (parameter of block) {put code here}, example:
int(^myblock) (intNUM1,intnum2) = ^int(intNUM1,intnum2) { return -;};
If your block does not require a return type and parameter, you can abbreviate it as:
void (^MYBLOCK2) () = ^(){ };
Or
void (^MYBLOCK2) (void) = ^void(void) { };
Returns a type or parameter that can be replaced with "void" if not.
You can also delete the equals sign to the right, ^ after (), which is:
void (^MYBLOCK2) () = ^{ };
You can also define a block function, but do not write the implementation of the function, we can later write the implementation of the specific function, like this:
void (^MYBLOCK2) (void= ^{ };
Second, block as a method definition
To define a block in a method, unlike the above, the block's name does not need to be written on the declaration, but in the back, like this:
-(void ) Getwtihblock: (void (^) ()) block{ // Span style= "margin:0px; padding:0px; Color:rgb (0,128,0); Line-height:1.5!important "> code ... // Remember to call block
How to use:
[Self getwtihblock:^{ NSLog (@ "sdf");}];
Here is an example of a detailed point, and write a note:
/** * append self string n times (add a newline before each copy \ n) * * @param string String * @param count append * @param stringblock target block, where St R parameter is the result string*/ //Blocks can also be defined in the method, but do not need to define the block's name//iOS develops a lot of APIs that also use blocks, like UIView's block animations- (void) Getstrwithstring: (NSString *)stringCopycount: (int) Count resultstring: (void(^) (NSString *str)) stringblock{nsmutablestring*newstring = [nsmutablestring stringwithstring:string]; for(Nsuinteger i =0; I < count; i++) {Nsuinteger len= [stringlength]; NSString*insertstring = [NSString stringWithFormat:@"\n%@",string]; [NewString insertstring:insertstring Atindex:len]; } //call block, pass in the string newstringStringblock (newstring);}
Usage is the same:
Blockobject *block =[[Blockobject alloc] init]; [Block getstrwithstring:@"Garvey"Copycount:3resultstring:^ (NSString *str) { //Str As a result of processingNSLog (@"Str is%@", str); }];
Sometimes the complex block syntax makes the declaration of a function difficult to read, so a typedef is often used to make a new type of block.
void (^resultblock) (NSString *str);
When you define a method, you become:
-(void) GetStrWithString2: (NSString *) string copycount: (int) Count resultstring: (Resultblock) Stringblock;
Let's compare the use of typedef before and after:
//before use- (void) Getstrwithstring: (NSString *)stringCopycount: (int) Count resultstring: (void(^) (NSString *str)) Stringblock;//after use- (void) GetStrWithString2: (NSString *)stringCopycount: (int) Count resultstring: (resultblock) Stringblock;
Note: The use method is the same, but the definition becomes simple.
IOS Block Basic usage