类型之间而不是变量之间的插入符,用括号括起来

发布于 2024-09-06 12:16:16 字数 189 浏览 4 评论 0原文

我在浏览 Apple 的文档时看到了类似 (void (^)(void)) 的内容。有人能解释一下这句话的意思吗? ^ 是异或,对吗? void XOR void 对我来说没有多大意义?

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

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?

There was also something like (void (^)(BOOL finished))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-09-13 12:16:16

这些块将匿名函数和函数对象添加到 Objective-C。请参阅介绍块和大中央调度

块对象(非正式地称为“块”)是 C、Objective-C 和 C++ 的扩展,使程序员可以轻松定义独立的工作单元。块与传统函数指针类似,但功能更强大。主要区别是:

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

声明块变量:

void (^my_block)(void);

将块对象分配给它:

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

调用它:

my_block(); // prints “hello world\n”

接受块作为参数:

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

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

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

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

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:

  • Blocks can be defined inline, as “anonymous functions.”
  • Blocks capture read-only copies of local variables, similar to “closures” in other languages

Declaring a block variable:

void (^my_block)(void);

Assigning a block object to it:

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

Invoking it:

my_block(); // prints “hello world\n”

Accepting a block as an argument:

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

Using that method with an inline block:

[obj doSomeThing:^(void){ printf("block was called"); }];
假情假意假温柔 2024-09-13 12:16:16

这是一个,Apple 特定的 C 扩展,类似于函数指针,或其他语言中的函数对象。

(void (^)(void)) 看起来像是对不带参数且不返回任何内容的块类型的类型转换。类似地,(void (^)(BOOL finish)) 看起来像是另一种类型转换,是一个带有一个布尔参数且不返回任何内容的块。

That's a block, an Apple-specific extension to C, similar to function pointers, or function objects in other languages.

(void (^)(void)) looks like a typecast to the type of a block that takes no parameters and returns nothing. Similarly, (void (^)(BOOL finished)) looks like another typecast, to a block with one boolean parameter and returning nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文