函数回调从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() // 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_my_function()
传递call_my_function()
的地址,然后call_my_function()
调用func_to_call()
。
从swift我正确看到函数test()
和func_to_call()
,但似乎是这样
void call_my_function(void (*callback_function)())
无法识别(使用未解析的标识符)如果我删除参数void (*callback_function)()
则再次识别该函数。
如何将Swift函数地址传递给C并将其调回? 可能吗? 谢谢
Apple在开发论坛上证实我现在不支持它,并要求我在bug报告器上填写新请求。
而且,我给读者另一个细节:
似乎在编译的二进制文件中,所有swift函数的符号已经可用并且桥接可以从C访问(即使在一个仅限swift的应用程序中)
我在swift文件中创建了一个名为FunctionTest,iPhone App的应用程序
func thisIsATestFunction(){println(“test”)}
编译,然后从终端:
nc /Users/xxx/Library/Developer/Xcode/DerivedData/FunctionTest-hhrbtzsuyrdoftfnbakosvenaiak/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
从c调用地址00003840执行该function
void (* func)() = 0x00003840; func(); // the swift function is executed
所以我认为这已经在进行中了...希望他们在下一个版本中实现这个function:-)