Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C到Swift的函数回调_C_Swift - Fatal编程技术网

从C到Swift的函数回调

从C到Swift的函数回调,c,swift,C,Swift,我有一个C函数,它只调用另一个作为参数传递的函数 void call_my_function(void (*callback_function)()) { callback_function(); } 这是C测试代码: void func_to_call() // a simple test function passed in as a callback { printf("function correctly called"); } void test() //

我有一个C函数,它只调用另一个作为参数传递的函数

void call_my_function(void (*callback_function)())
{
    callback_function();
}
这是C测试代码:

void func_to_call()    // a simple test function passed in as a callback
{
    printf("function correctly called");
}

void test()   // entry point
{
    void (*foo)();
    foo = &func_to_call;
    call_my_function(foo);     // pass the address of "func_to_call()" to "call_my_function()"
}
本质上,从
test()
,我调用
call\u my\u function()
func\u的地址传递给\u call()
,然后
调用\u my\u function()
回调
func\u to\u call()

从swift中,我正确地看到了函数
test()
func\u to\u call()
,但似乎

void call_my_function(void (*callback_function)())
无法识别(使用未解析的标识符) 如果我删除参数
void(*callback_function)(
),则该函数将再次被识别

如何将Swift函数地址传递给C并将其回调?可能吗?
感谢

苹果在开发论坛上向我确认,现在不支持它,并要求我在bugreporter上填写新的请求

此外,我还向读者提供了另一个细节:

似乎在编译的二进制文件中,所有swift函数的符号都已经可用,并且可以通过桥接从C访问(即使是在只有swift的应用程序中)

我制作了一个名为FunctionTest的应用程序,iPhone应用程序在swift文件中具有此功能

func thisIsATestFunction() { println(“测试”) }

编译,然后从终端:

nc/Users/xxx/Library/Developer/Xcode/DerivedData/FunctionTest hhrbtzsuyrdoftfnbakosvenaik/Build/Products/Debug iphonesimulator/FunctionTest.app/FunctionTest

     U _NSStringFromClass
     U _OBJC_CLASS_$_NSString
     U _OBJC_CLASS_$_UIResponder
     U _OBJC_CLASS_$_UIViewController
     U _OBJC_CLASS_$_UIWindow
000088c8 S _OBJC_CLASS_$__TtC12FunctionTest11AppDelegate
00008888 S _OBJC_CLASS_$__TtC12FunctionTest14ViewController
.........
.........
00003840 T __TF12FunctionTest19thisIsATestFunctionFT_T_          <--- this is my test function

因此,我认为这项工作已经在进行中……希望他们能在下一个版本中实现此功能:-)

和?现在有没有办法从swift闭包中获取C函数指针?请检查,因为它以更安全的方式解决了这个问题。
void (* func)() = 0x00003840;
func();    // the swift function is executed