C to Swift Callback Function

I have this C function that just calls another function passed as parameter

void call_my_function(void (*callback_function)())
{
    callback_function();
}

This is test code 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()"
}

Essentially, from test(), I call call_my_function(), passing in the address func_to_call(), and then call_my_function()accessing func_to_call().

With fast, I see functions correctly test()and func_to_call(), but it seems that

void call_my_function(void (*callback_function)())

not recognized (using an unresolved identifier) ​​If I delete a parameter void (*callback_function)(), the function is recognized again.

What can I do to pass the address of the Swift function to C and return it? Is it possible? Thanks

+4
source share
1 answer

Apple , , bugreporter.

, :

, , C ( )

FunctionTest, iPhone App

func thisIsATestFunction() {   Println ( "" ) }

, :

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

void (* func)() = 0x00003840;
func();    // the swift function is executed

, , ... , : -)

+2

Source: https://habr.com/ru/post/1543756/


All Articles