IOS Block Basic usage

Source: Internet
Author: User

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

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.