将块传递给 C++目标C的方法

发布于 2024-11-07 14:31:05 字数 237 浏览 0 评论 0原文

我有一个与 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 技术交流群。

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

发布评论

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

评论(2

赤濁 2024-11-14 14:31:05

那么只是你不完全熟悉块语法吗?如果是这样,如果您已经熟悉函数指针,那么这里有一个简单的示例,希望它有意义(语法或多或少相同,但使用 ^ 来声明一个 [创建当然,闭包是不同的])。

您可能想为块类型设置一个 typedef,只是为了避免一遍又一遍地重复相同的事情,但我提供了使用 typedef 和仅将块类型本身放入参数中的示例。

#import <Cocoa/Cocoa.h>

// do a typedef for the block
typedef void (^ABlock)(int x, int y);

class Receiver
{
public:
    // block in parameters using typedef
    void doSomething(ABlock block) {
        block(5, 10);
    }

    // block in parameters not using typedef
    void doSomethingToo(void (^block)(int x, int y)) {
        block(5, 10);
    }
};


int main (int argc, char const *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    Receiver rcv;
    // pass a block
    rcv.doSomething(^(int x, int y) { NSLog(@"%d %d", x, y); });
    rcv.doSomethingToo(^(int x, int y) { NSLog(@"%d %d", x, y); });

    [pool drain];
    return 0;
}

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.

#import <Cocoa/Cocoa.h>

// do a typedef for the block
typedef void (^ABlock)(int x, int y);

class Receiver
{
public:
    // block in parameters using typedef
    void doSomething(ABlock block) {
        block(5, 10);
    }

    // block in parameters not using typedef
    void doSomethingToo(void (^block)(int x, int y)) {
        block(5, 10);
    }
};


int main (int argc, char const *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    Receiver rcv;
    // pass a block
    rcv.doSomething(^(int x, int y) { NSLog(@"%d %d", x, y); });
    rcv.doSomethingToo(^(int x, int y) { NSLog(@"%d %d", x, y); });

    [pool drain];
    return 0;
}
凤舞天涯 2024-11-14 14:31:05

请参阅 使用块作为函数参数。声明方法时,请使用类似于以下的语法来声明块参数:

void theFunction(blockReturnType (^argumentName)(blockArgumentTypes));

块调用看起来像函数调用,因此上面的 theFunction 实现仅调用该块并返回其结果如下所示:

int theFunction(int anArgument, int (^theBlock)(int)) {
    return theBlock(anArgument);
}

此语法适用于 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:

void theFunction(blockReturnType (^argumentName)(blockArgumentTypes));

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:

int theFunction(int anArgument, int (^theBlock)(int)) {
    return theBlock(anArgument);
}

This syntax will work for C, C++, and Objective-C.

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