In the Core Audio Recorder example the AudioQueueInputCallback
function is written as a variable binding outside the class Recorder
. I am trying to use it inside a struct, but I am not able to access any instance methods. It gives the error, Instance members cannot be used on type.
struct Recorder {
private var log = Logger()
private let utils = Utils()
func record() {
// ...
AudioQueueNewInput(&recordFormat, audioQueueInputCallback, &recorder, nil, nil, 0, &queue)
}
private let audioQueueInputCallback: AudioQueueInputCallback = { (inUserData: UnsafeMutableRawPointer?, inQueue: AudioQueueRef,
inBuffer: AudioQueueBufferRef, inStartTime: UnsafePointer<AudioTimeStamp>,
inNumPackets: UInt32, inPacketDesc: UnsafePointer<AudioStreamPacketDescription>?) in
log.debug() // <-- error: instance member cannot be used on type Recorder
}
How to write audioQueueInputCallback
inside a struct such that the instance variables can be accessed inside it?
Update: If I change the var to lazy as:
private lazy var audioQueueInputCallback: AudioQueueInputCallback = {
(_ inUserData: UnsafeMutableRawPointer?, _ inQueue: AudioQueueRef,
_ inBuffer: AudioQueueBufferRef, _ inStartTime: UnsafePointer<AudioTimeStamp>,
_ inNumPackets: UInt32, _ inPacketDesc: UnsafePointer<AudioStreamPacketDescription>?) in
log.debug("audio queue callback")
}
I am getting Closure cannot implicitly capture a mutating self parameter
error.