I am playing with Swift and noticed that Swift does not allow to create CFFunctionPointers. It can only pass around and reference existing ones.
As for example CoreAudio requires CFunctionPointer to certain callbacks therefore I cannot use pure Swift.
So I need to use some Objective-C trampoline or wrapper here that takes a Swift Closure as a parameter as well as the original callback prototype and then can be assigned to be the callback, but the actually action happens in Swift and not Objective-C.
How do I do this?
Some example code for such a wrapper would help me to understand how I can use Swift code from objective C for such purposes in a flexible way to work around Swift not being able to create CFunctionPointers.
Yes, I know I can just write stuff when needed in Objective-C. I want to do it in pure Swift as a learning exercise porting one of my apps to Swift (uses a lot of CoreAudio/CoreVideo framework).
I needed to define this callback:
and I wanted to use Objective-C as least as possible.
This was my approach:
MIDIReadProcCallback.h
MIDIReadProcCallback.m
Then you can register
MIDIReadProcCallback.midiReadProc
as callback and set handlerMIDIReadProcCallback.setOnCallback({ (packetList: MIDIPacketList) in ... })
Well, you can create a function pointer.
It doesn't work though with Core MIDI.
BTW., you cannot have a bridging header if your MIDI code is in a framework you're writing.