A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 阿么 中级黑马   /  2016-3-16 21:53  /  1062 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#import <Foundation/Foundation.h>
//定义一个别名 BlockType
typedef void (^BlockType)();
typedef void (^BlockType1)(int a,int b);
typedef int (^BlockType2)(int a,int b);

//定义返回值是BlockType类型的函数
BlockType test(){
   
    //定义block变量 b1
    void (^b1)() = ^{
        
        int a = 10;
        int s = a+100;
        NSLog(@"s = %d",s);
        
    };
   
    //b1();
   
    return b1;
}

//定义返回值是BlockType1的函数
BlockType1 test1(){

    return ^(int a,int b){
   
        NSLog(@"a+b = %d",a+b);
   
    };

}


BlockType2 test2(){
    //return 函数返回值
    return ^(int a,int b){
        //return block代码块的返回值
        return a+b;
    };
   
}


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //定义BlockType 类型的变量,接收test函数返回的结果i
        BlockType bb = test();
        bb(); //执行 block
        
        BlockType1 b2 = test1();
        b2(34,10);
        
        BlockType2 b3 = test2();
        //b3接收了函数的返回值
        //因为函数的返回值是一个有参数,有返回值的block
        //所以,b3可以直接执行block,同时block返回值是int类型
        //故,s = b3(10,38);
        int s = b3(10,38);
        NSLog(@"s = %d",s);
        
    }
    return 0;
}

2 个回复

倒序浏览
太长  不看  直接点赞系列太长  不看  直接点赞系列太长  不看  直接点赞系列太长  不看  直接点赞系列太长  不看  直接点赞系列
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马