-1

I have the following C function

struct answer* create(struct callbacks *callbacks);

I need to create a struct of function pointers and call this function from Swift

What I have so far is:

func aa(state: CInt, user_data: UnsafeMutablePointer<Void>) {}
func bb(state: CInt, user_data: UnsafeMutablePointer<Void>) {}
struct CallbacksStruct {
     let on_aa = aa
     let on_bb = bb
}

and I try to call the function like this:

var callbackStruct = CallbacksStruct()
let ans = create(&callbackStruct)

but, without any success

Is it even possible in Swift ?

Witterquick
  • 6,048
  • 3
  • 26
  • 50

1 Answers1

2

Yes, it should be possible. In Swift 2, one can send a Swift closure as a parameter to C, when C expects a function pointer. In your case, you have wrapped it in a struct, so an addition of '@convention(c)' to explicitly state that it's C, might help.

see examples here new-conventionc-in-swift-2-how-can-i-use-it

Community
  • 1
  • 1
Ethan Halprin
  • 470
  • 3
  • 11