标签:
blocks 这东西就是个函数指针和匿名函数。
示例代码
// // main.m // CompletionBlock // // Created by liubing on 15/9/23. // Copyright © 2015年 QuentinLabs. All rights reserved. // #import <Foundation/Foundation.h> typedef void(^CompletionBlock)(); typedef void(^CompletionBlockInt)(int); @interface SampleClass : NSObject -(void)performActionWithCompletion:(CompletionBlock)completionBlock; -(void)performActionWithCompletionInt:(CompletionBlockInt)completionBlockInt withInt:(int) argInt; @end @implementation SampleClass -(void) performActionWithCompletion:(CompletionBlock)completionBlock { NSLog(@"Action Performed"); completionBlock(); } -(void)performActionWithCompletionInt:(CompletionBlockInt)completionBlockInt withInt:(int) argInt; { NSLog(@"Action Performed "); completionBlockInt(argInt); } @end int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... // NSLog(@"Hello, World!"); SampleClass* sampleClass = [[SampleClass alloc]init]; [sampleClass performActionWithCompletion:^ { NSLog(@"Completion is called to intimate action is performed."); }]; [sampleClass performActionWithCompletionInt: ^(int argValue) { NSLog(@"Completion is Called to %i",argValue); } withInt:1024]; } return 0; }
10.10.5 + XCode 7 下编译通过
教程地址 http://www.tutorialspoint.com/objective_c/objective_c_blocks.htm
注: http://www.tutorialspoint.com 是个蛮不错的教程网站。
标签:
原文地址:http://www.cnblogs.com/lbfamous/p/4833280.html