typedef和Block

Posted iFat的笔记本

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typedef和Block相关的知识,希望对你有一定的参考价值。

1.函数指针回顾

  • 函数指针使用
int sum(int value1, int value2)
{
    return value1 + value2;
}

int minus(int value1, int value2)
{
    return value1 - value2;
}

int main(int argc, const char * argv[]) {
    int (*sumP) (int, int) = sum;
    int res = sumP(10, 20);
    NSLog(@"res = %i", res);

    int (*minusP) (int , int) = minus;
    res = minusP(10, 20);
    NSLog(@"res = %i", res);
    return 0;
}
  • 函数指针别名
typedef int (*calculate) (int, int);
int main(int argc, const char * argv[]) {
    calculate sumP = sum;
    int res = sumP(10, 20);
    NSLog(@"res = %i", res);
    calculate minusP = minus;
    res = minusP(10, 20);
    NSLog(@"res = %i", res);
    return 0;
}

2.block和typedef

  • block使用

int main(int argc, const char * argv[]) {
  int (^sumBlock) (int, int) = ^(int value1, int value2){
      return value1 + value2;
  };
  int res = sumBlock(10 , 20);
  NSLog(@"res = %i", res);

  int (^minusBlock) (int, int) = ^(int value1, int value2){
      return value1 - value2;
  };
  res = minusBlock(10 , 20);
  NSLog(@"res = %i", res);
  return 0;
}
  • block别名

int main(int argc, const char * argv[]) {
    calculateBlock sumBlock = ^(int value1, int value2){
        return value1 + value2;
    };
    int res = sumBlock(10, 20);
    NSLog(@"res = %i", res);
    calculateBlock minusBlock = ^(int value1, int value2){
        return value1 - value2;
    };
    res = minusBlock(10, 20);
    NSLog(@"res = %i", res);

    return 0;
}

 

以上是关于typedef和Block的主要内容,如果未能解决你的问题,请参考以下文章

block创建时出现Typedef redefinition with different types错误

iOS开发之---block的使用(详解)

iOS block=写法总结(如block的参数是一个block)

ios block和delegate的区别

Sphinx - 在代码块片段中使用省略号 (...)

iOS学习之代码块(Block)