类型之间的尖号字符,而不是变量,用括号括起来 [英] Caret character between types rather than variables, surrounded by parentheses

查看:163
本文介绍了类型之间的尖号字符,而不是变量,用括号括起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Apple的文档,并且看到了类似(void (^)(void))的内容.有人可以解释这句话的意思吗? ^是XOR,对吗? void XOR void对我来说没有多大意义?

I was going through Apple's documentation and I saw something like this (void (^)(void)). Can somebody explain what this statement means? ^ is XOR, right? void XOR void doesn't makes much sense to me?

还有类似(void (^)(BOOL finished))

推荐答案

这些是将匿名函数和函数对象添加到Objective-C的块.参见例如引入积木和盛大中央调度:

These are blocks which add anonymous functions and function objects to Objective-C. See e.g. Introducing Blocks and Grand Central Dispatch :

块对象(非正式地称为块")是C以及Objective-C和C ++的扩展,使程序员可以轻松地定义独立的工作单元.块与传统函数指针类似,但功能比传统函数指针强大得多.关键区别在于:

Block objects (informally, "blocks") are an extension to C, as well as Objective-C and C++, that make it easy for programmers to define self-contained units of work. Blocks are similar to — but far more powerful than — traditional function pointers. The key differences are:

  • 块可以内联定义为匿名函数".
  • 块捕获局部变量的只读副本,类似于其他语言中的"closures"

声明一个块变量:

void (^my_block)(void);

为其分配块对象:

my_block = ^(void){ printf("hello world\n"); };

调用:

my_block(); // prints "hello world\n"

接受块作为参数:

- (void)doSomething:(void (^)(void))block;

将该方法与内联块一起使用:

Using that method with an inline block:

[obj doSomeThing:^(void){ printf("block was called"); }];

这篇关于类型之间的尖号字符,而不是变量,用括号括起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆