IOS development-Object-C Block implementation method, ios-object-c

Source: Internet
Author: User

IOS development-Object-C Block implementation method, ios-object-c

We can regard Block as a closure function, which can access external variables and local variables, but the external variables cannot be modified by default. You can use it for callback, which is more intuitive than using a proxy (Delegate. By the way, many apple APIs use blocks.

 

 

I. basic definition of Block

Basic Block Writing Method (also detailed ):

returnType (^blockName)(params) = ^returnType(params) {    // code... };

Explanation: return type (^ Block name) (Block parameter) = ^ return type (Block parameter) {put code here}, for example:

int (^myBlock)(int num1, int num2) = ^int(int num1, int num2){    return 100;};

 

If your Block does not need to return types and parameters, You Can abbreviated it:

void (^myBlock2)() = ^(){    };

Or

void (^myBlock2)(void) = ^void(void){    };

Return type or parameter. If not, use "void" instead.

 

You can also delete the () after ^ on the right of the equal sign, that is:

void (^myBlock2)() = ^{    };

Is this simple?

 

You can also define a Block function without writing the function implementation. We can write the implementation of a specific function later, as shown in the following figure:

void (^myBlock2)(void);myBlock2 = ^{    };

 

 

Ii. Block as method definition

Define the Block in the method. Different from the above, the Block name does not need to be written in the declaration, but is later, like this:

-(Void) getWtihBlock :( void (^) () block {// code... // remember to call block ();}

 

Usage:

[self getWtihBlock:^{    NSLog(@"sdf");}];

 

The following is an example of verbose and a note:

/*** Append its own string N times (a newline \ n is added before each copy) ** @ param string * @ param count append times * @ param stringBlock target Block, the str parameter is the result string * // Block, which can also be defined in the method, but the Block name does not need to be defined. // many APIs developed by IOS also use Block, block animation like UIView-(void) getStrWithString :( NSString *) string CopyCount :( int) count resultString :( void (^) (NSString * str )) stringBlock {NSMutableString * newString = [NSMutableString stringWithString: string]; for (NSUInteger I = 0; I <count; I ++) {NSUInteger len = [string length]; NSString * insertString = [NSString stringWithFormat: @ "\ n % @", string]; [newString insertString: insertString atIndex: len];} // call the block, input string newString stringBlock (newString );}

 

The usage is the same:

BlockObject * block = [[BlockObject alloc] init]; [block getStrWithString: @ "Garvey" CopyCount: 3 resultString: ^ (NSString * str) {// str is the processed result NSLog (@ "str is % @", str);}];

 

Sometimes the complex Block syntax makes the declaration of the function difficult to read, so typedef is often used to create a new type for the Block.

typedef void (^ResultBlock)(NSString *str);

 

The method is defined as follows:

- (void)getStrWithString2:(NSString *)string                CopyCount:(int)count             resultString:(ResultBlock)stringBlock;

 

Let's make a comparison. Before and after using typedef:

// Use the prefix-(void) getStrWithString :( NSString *) string CopyCount :( int) count resultString :( void (^) (NSString * str) stringBlock; // after use-(void) getStrWithString2 :( NSString *) string CopyCount :( int) count resultString :( ResultBlock) stringBlock;

Note: The usage is the same, but the definition is simplified.

 

 

If you have been using a proxy (Delegate) for method callback, you can now try to use the Block function.

 

 

Blog Author: GarveyCalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation!

 

Related Article

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.