【发布时间】:2019-07-24 23:48:52
【问题描述】:
在Core Audio Recorder example 中,AudioQueueInputCallback 函数被编写为Recorder 类之外的变量绑定。我试图在结构中使用它,但我无法访问任何实例方法。它给出了错误,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
}
如何在结构中写入audioQueueInputCallback,以便可以在其中访问实例变量?
更新:如果我将 var 更改为惰性:
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")
}
我收到Closure cannot implicitly capture a mutating self parameter 错误。
【问题讨论】:
标签: ios swift closures core-audio