OC中块Block的用法

来源:互联网 发布:网络被攻击了怎么办 编辑:程序博客网 时间:2024/05/18 01:22

因为对这个概念比较陌生,以前没接触过,做一下笔记方便以后回忆.

块的用法.


#import<Foundation/Foundation.h>@interface Person:NSObject@end@implementation Person@endint main(int argc,char  *argv[]){@autoreleasepool{     //block,块这种东西,其实就是一段代码而已长得想没有名字的函数//块可以作为其他函数的参数传进去,就等于传一段代码进去//第1个块示例void(^display1)(int ,int)=^(int a,int b){NSLog(@"block of display1: %d %d",a,b);};//第1个块调用display1(1,2);//第2个块定义int (^display2)(NSString*);display2=^(NSString* message){NSLog(@"block of display2: %@",message);return 1;};//第2个块调用int a=display2(@"i am display2");NSLog(@"%d",a);}return 0;}




0 0