'How to call a Swift function with a callback from C

I am trying to write an iOS plugin for Unity, which requires the use of a bridge with a C layer.

The plugin itself is written in Swift.

Currently my implementation works like so:

// Swift

@objc public class SomeClass  {

    @objc public func SomeFunction() -> String {
        return "Hello"
    }

}

// C

extern "C" {

    void _someFunction() {
        // I can access the Swift file using "[SomeClass shared]"
        // This returns the "Hello" string correctly
        NSString *helloMessage = [[SomeClass shared] SomeFunction];
    }

}

This works, but now I need to make an async call which requires a callback from the Swift implementation.

// Swift

@objc public class SomeClass  {

    @objc public func SomeFunction(completionHandler: (String) -> Void) {
        // Gets called after some async operations
        completionHandler(“hello”)
    }

}

Is it possible to call this Swift function from the C layer and how would you do so?

// C

extern "C" {

    void _someFunction() {
        // ??????
    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source