How do I define a MIDINotifyProc in Swift?
Asked Answered
L

1

1

I'm trying to get MIDI sessions working with swift code and am running into issues with some of the delegates.

This is a working example in Objective-C.

OSStatus status = MIDIClientCreate(CFSTR("MIDI Client"), MIDIStateChangedHandler, nil, &client);

void MIDIStateChangedHandler(const MIDINotification *message, void *refCon)
{
    NSLog(@"MIDIStateChanged!");
}

This is what I'm trying with Swift:

var status = MIDIClientCreate("the client", notifyProc: MIDIStateChangedHandler, notifyRefCon: nil,  outClient: client) 

func MIDIStateChangedHandler(message: MIDINotification, refCon: UnsafeMutablePointer<Void>)
{
    println("MIDIStateChanged!");
}

This is the error which I cannot figure out (I'm new to iOS with many years of C# experience):

cannot convert the expression's type '(StringLiteralConvertible, notifyProc: (MIDINotification, refCon: UnsafeMutablePointer) -> (), notifyRefCon: NilLiteralConvertible, outClient: @lvalue MIDIClientRef!)' to type 'StringLiteralConvertible'

var status = MIDIClientCreate("the client", notifyProc: MIDIStateChangedHandler, notifyRefCon: nil, outClient: client)

Apple does not have a Swift example for MIDINotifyProc or other MIDI functions I want to use and I can't figure out the correct method parameter type.

Linc answered 14/11, 2014 at 7:52 Comment(0)
J
1

Currently there is no way convert Swift functions to C functions, so it is not possible to do this only with Swift. Here an example on how to do it using Objective-C as least as possible: Objective-C Wrapper for CFunctionPointer to a Swift Closure

Joselyn answered 14/11, 2014 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.