1 Preface
This article describes how to call block object by using functions and how to call block object by using block object.
2. code example
Testdemo. h
#import <Foundation/Foundation.h>@interface TestDemo : NSObject- (void) callSimpleBlock;- (void) callTrimBlock;@end
Testdemo. m
# Import "testdemo. H "@ implementation testdemo/**************** method to call block object start ************** */void (^ simpleblock) (nsstring *) = ^ (nsstring * paramstring) {/* implement the block object here and use the paramstring parameter */nslog (@ "% @", paramstring );}; -(void) callsimpleblock {simpleblock (@ "Archy ");} ***************//** * ************ block object call block object start ******************/nsstring * (^ trimstring) (nsstring *) = ^ (nsstring * inputstring) {nsstring * result = [inputstring character: [nscharacterset whitespacecharacterset]; return result ;}; nsstring * (^ trimwithotherblock) (nsstring *) = ^ (nsstring * inputstring) {return trimstring (inputstring) ;};-(void) calltrimblock {nsstring * trimmedstring = trimwithotherblock (@ "Archy "); nslog (@ "Trimmed string = % @", trimmedstring );} /*************** block object call block object start *****************/@ end
Main. m
int main(int argc, const char * argv[]){ @autoreleasepool { TestDemo *test = [[TestDemo alloc] init];// [test callSimpleBlock]; [test callTrimBlock]; } return 0;}
Running result
06:53:50. 893 callblockobjecttest [591: 303] trimmed string = Archy
3 conclusion
The above is all content and I hope it will help you.
Demo download: http://download.csdn.net/detail/u010013695/5351963