首页 > 其他 > 详细

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

时间:2014-11-26 22:08:42      阅读:1123      评论:0      收藏:0      [点我收藏+]

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。来假设。

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

原文:http://www.cnblogs.com/asheng/p/4124879.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!