Objective-C Wrapper for CFunctionPointer to a Swift Closure

后端 未结 2 1686
失恋的感觉
失恋的感觉 2020-12-03 15:53

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 Core

2条回答
  •  隐瞒了意图╮
    2020-12-03 16:25

    Well, you can create a function pointer.

    var ump = UnsafeMutablePointer<((UnsafePointer, UnsafeMutablePointer, UnsafeMutablePointer ) -> Void)>.alloc(1)
    ump.initialize(MyMIDIReadProc)
    let cp = COpaquePointer(ump)
    let fp = CFunctionPointer<((UnsafePointer, UnsafeMutablePointer, UnsafeMutablePointer ) -> Void)>(cp)
    
    status = MIDIDestinationCreate(midiClient,
            name,
            fp,
    etc.
    

    It doesn't work though with Core MIDI.

    thread #7: tid = 0x713b7, 0x7a1541f0, stop reason = EXC_BAD_ACCESS (code=2, address=0x7a1541f0)
    frame #0: 0x7a1541f0
    frame #1: 0x00159295 CoreMIDI`LocalMIDIReceiverList::HandleMIDIIn(void*, OpaqueMIDIEndpoint*, void*, MIDIPacketList const*) + 117
    

    BTW., you cannot have a bridging header if your MIDI code is in a framework you're writing.

提交回复
热议问题