将块传递给 C++目标C的方法
我有一个与 Objective-C 一起使用的 C++ 帮助程序类。我想从视图控制器(回调)向 c++ 类传递一个块,以便在执行它时我位于主线程上并且可以更新 UI。我目前有一个类似的系统,在调用函数时使用函数指针和 performSelector
。我想要一个有关如何设置 C++ 变量以及如何将 Objective-C 块传递给它并从 C++ 类调用它的示例。
如果这是不可能的,你能想到另一个/更好的解决方案吗?
I have a C++ helper class that I use with objective-C. I would like to pass the c++ class a block from a view controller (a callback) so that when it is executed I am on the main thread and can update the UI. I currently have a similar system working with function pointers and performSelector
when the function is called. I would like an example of how to set up the c++ variable and how to pass an objective-C block to it and call it from the c++ class.
If this is not possible, can you think of another/better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么只是你不完全熟悉块语法吗?如果是这样,如果您已经熟悉函数指针,那么这里有一个简单的示例,希望它有意义(语法或多或少相同,但使用
^
来声明一个 [创建当然,闭包是不同的])。您可能想为块类型设置一个 typedef,只是为了避免一遍又一遍地重复相同的事情,但我提供了使用 typedef 和仅将块类型本身放入参数中的示例。
So is it just that you're not entirely familiar with the block syntax? If so, here's a quick example that should hopefully make sense if you're already familiar with function pointers (the syntax is more or less the same, but with the use of an
^
for declaring one [creating a closure is, of course, different]).You probably want to set up a typedef for the block types just to save yourself repeating the same thing over and over, but I included examples for both using a typedef and just putting the block type itself in the parameters.
请参阅 使用块作为函数参数。声明方法时,请使用类似于以下的语法来声明块参数:
块调用看起来像函数调用,因此上面的
theFunction
实现仅调用该块并返回其结果如下所示:此语法适用于 C、C++ 和 Objective-C。
See Using a Block as a Function Argument. When declaring your method, use a syntax similar to the following to declare a block argument:
A block call looks like a function call, so an implementation of
theFunction
above which simply calls the block and returns its result would look like this:This syntax will work for C, C++, and Objective-C.