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


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

    纸上得来终觉浅,绝知此事要躬行
  • 相关阅读:
    洛谷P1036 选数
    洛谷 P1009 阶乘之和
    codevs 4165 ​高精度求阶乘
    codevs 1553 互斥的数
    P2421 A-B数对(增强版)
    51nod 1081 子段求和
    codevs 3054 高精度练习-文件操作
    无聊写的高精的斐波那契数列
    51nod 1347 旋转字符串
    51nod 1212 无向图最小生成树(Kruskal模版题)
  • 原文地址:https://www.cnblogs.com/asheng/p/4124879.html
Copyright © 2020-2023  润新知