您的位置:首页 > 其它

关于oc中出现的typedef的用法/定义函数指针

2017-04-16 13:53 555 查看
typedef int (^calculateBlock)(int a,int b);
这里面typedef的作用只是给 calculateBlock取了一个 别名,说明以后可以直接使用。

calculateBlock类似于*所以可以有类似的用法,以后可以直接使用calculateBlock来定义方法。例:

- (int)calculateWithNumber1:(int)number1 andNumber:(int)number2 andCalculate:(calculateBlock)calculate;

直接使用calculateBlock来定义作为方法的参数。做到的调用为:

- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate

{

    //经常变化的功能,在设计当中叫做封装变化

    return calculate(number1,number2);

}

 

在main中代码为:

  int (^sumBlock)(int a, int b) = ^int (int a, int b) {

            

            int result = (a * b);

            x = result;

            return result;

        };

 调用自己定义的方法:

 int sum =[cal calculateWithNumber1:10 andNumber:20 andCalculate:sumBlock];

 

在不知道使用什么样的方式计算时可以使用传入block。来假设。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: