当前位置: 首页 > 工具软件 > iOS Block UI > 使用案例 >

iOS Block使用

秦学林
2023-12-01

// 定义block

//1.无参数无返回值的Block

typedef void(^chengfa)();

//2.有参数无返回值的Block

typedef void(^jianfa)(int a, int b );

//3.有参数有返回值的Block

typedef int(^jiafa)(int a, int b );

// 声明block

@property (nonatomic,strong)jiafa  plus;

@property (nonatomic,strong)jianfa  jq;

@property (nonatomic,strong)chengfa  cheng;



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    self.jq = ^(int a, int b) {

        NSLog(@"减法 = %zd",a-b);

    };

    self.jq(9, 4);

    self.plus = ^int(int a, int b) {

        return a+b;

    };

    NSLog(@"加法 = %zd",self.plus(5, 6)) ;

    self.cheng = ^{

        NSLog(@"无参数无返值");

    };

    self.cheng();

    void (^emptyBlock)() = ^(){

        NSLog(@"无参数,无返回值的Block");

    };

    emptyBlock();

}





 类似资料: