简体   繁体   English

将 Objective-C 方法传递给 C++ 方法

[英]Passing Objective-C method to c++ method

I created void type method in Objective-C to pass it to C++ method shown below:我在 Objective-C 中创建了 void 类型方法将其传递给 C++ 方法,如下所示:

C++ method to which Objectiv-C method will be passed:将传递 Objectiv-C 方法的 C++ 方法:

glfwSetWindowSizeCallback(m_Window, windowResize);

Here is Objective-C method:这是Objective-C方法:

- (void) windowResize:(GLFWwindow *)window :(int)width :(int)height {glViewport(0, 0, width, height);}

I know it can be solved by adding C++ file to the project and linking it to .m file with C++ method, but i would like to implement it in Cocoa syntax我知道可以通过将 C++ 文件添加到项目中并使用 C++ 方法将其链接到 .m 文件来解决它,但我想用 Cocoa 语法实现它

ObjC methods can be passed arround as selector s like ObjC 方法可以像选择器一样传递

SEL method = @selector(windowResize:width:height:);
glfwSetWindowSizeCallback(m_Window, method);

Then applied inside glfwSetWindowSizeCallback.然后在 glfwSetWindowSizeCallback 内部应用。 Alternately you could wrap the ObjC call in ac function and pass the function pointer.或者,您可以将 ObjC 调用包装在 ac 函数中并传递函数指针。

A more complete example might be:一个更完整的例子可能是:

void func(NSApplication * target, SEL method) {
    if ([target respondsToSelector: method] ) {
       [target performSelector:method];
    }
}

elsewhere:别处:

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    SEL method = @selector(unhideAllApplications:);
    func([NSApplication sharedApplication] , method);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM