Reputation: 128
It's not a duplicate cause with lazy i have an issu too : "A C function pointer cannot be formed from a closure that captures context"
In my main class I have "detectChangeMidi" but in this code, when I try to call a function I don't understand why I can't. (i can't use var too, anything of my class)
I'm not expert in swift, then try to explain to me what's going one. I use the CoreMidi librarie.
UPDATE :
I replace the code by minimaliste code for better entendement.
import Foundation
import CoreMIDI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
gestionMidi()
//...
}
func gestionMidi() {
//...
let midiNotif:MIDINotifyProc = detectChangeMidi
MIDIClientCreate("Swift3 Test Client" as CFString, midiNotif, nil, &midiClient)
//...
}
func plop(){
print("bla bla bla")
}
let detectChangeMidi: @convention(c) (UnsafePointer<MIDINotification>, UnsafeMutableRawPointer?) -> Swift.Void =
{ midiNotification,unkown in
var notification = midiNotification.pointee
self.plop() //problem here
//...
}
}
Upvotes: 0
Views: 416
Reputation: 536009
Your entire agenda here is misguided; it is not at all obvious what you can be trying to do. You cannot declare a property or function as being convention(c)
and also refer to self
. But you don't need to! If your goal is to pass a function as a parameter where a C pointer-to-function is expected, just pass the function. Anyway you'll have a much easier time in Swift if you call MIDIClientCreateWithBlock(_:_:_:)
instead.
Upvotes: 1
Reputation: 6190
The problem is with the scope as I can judge from the error message.
Also the thing to get attention to is that you're executing plop()
in closure, and that requires self.plop()
.
also midiNotification,unkown in
- unkown
seems to be a typo.
Check where you declare the function itself. From this snippet it's hard to understand what's your declaration scope.
Upvotes: 0