来自另一种语言的我对一个无声的错误感到惊讶,其中一个作为回调突然传递给方法的对象永远不会被调用。对回调的引用不知何故丢失了。
问题的最小(不可运行)示例:
class Foo: NSObject, AVCaptureFileOutputRecordingDelegate {
func bar() {
let out = AVCaptureMovieFileOutput()
let delegate = Foo() //nonsensical in this case, in normal case diff. object will be used
out.startRecording(to: /*...*/, recordingDelegate: delegate)
//Result: delegate methods are never called
}
}
“解决方案”的最小(不可运行)示例:
class Foo: NSObject, AVCaptureFileOutputRecordingDelegate {
func bar() {
let out = AVCaptureMovieFileOutput()
out.startRecording(to: /*...*/, recordingDelegate: self)
//Result: delegate methods are called when approperiate
}
}
我很困惑...
- 为什么会这样?
- 如何预防这种情况?
- 这种无声的失败是设计使然吗?
这个问题源于AVCaptureMovieFileOutput never call delegate on screen recording