《Objective-C高级编程 iOS与OS X多线程与内存管理》10

Blocks篇:1.Blocks的声明

Blocks是带有自动变量值的匿名函数。

写法:

  • Blocks变量声明:与C函数指针声明基本相似,将指针符号"*"替换为“^”。
  • Blocks主体声明:省略了名称的C函数,在原函数名前添加“^”。
    • 无参数Blocks,可以省略参数列表
    • 无返回值Blocks,可以省略返回值

举例子:

// 完整声明
void (^myBlock)(int a) = void ^(int a) {
    ...
}

// 无参数,无返回值
void (^myBlock2)(void) = ^{
    ...
}

猜你喜欢

转载自blog.csdn.net/weixin_34099526/article/details/87569687