条件绑定的初始化器必须具有可选类型,而不是“AVAudioInputNode”

时间:2021-02-03 17:05:30

What causes the error stated above and how to fix it ?

是什么原因导致了上面所说的错误,以及如何修复它?

func cancelRecording() {
        audioEngine.stop()
        if let node = audioEngine.inputNode {
            node.removeTap(onBus: 0)
        }
        recognitionTask?.cancel()
    }

1 个解决方案

#1


1  

The compiler is telling you that you can't use an if let because it's totally unnecessary. You don't have any optionals to unwrap: audioEngine is not optional, and the inputNode property isn't optional either. if let is used exclusively to unwrap optionals. If you want to create a new constant named node, just do it:

编译器告诉你,你不能使用if let,因为它完全没有必要。您没有要展开的选项:audioEngine不是可选的,inputNode属性也不是可选的。如果让我们只使用unwrap选项。如果您想要创建一个新的名为node的常量,只需这样做:

if let node = audioEngine?.inputNode {
   node.removeTap(onBus: 0)
}

#1


1  

The compiler is telling you that you can't use an if let because it's totally unnecessary. You don't have any optionals to unwrap: audioEngine is not optional, and the inputNode property isn't optional either. if let is used exclusively to unwrap optionals. If you want to create a new constant named node, just do it:

编译器告诉你,你不能使用if let,因为它完全没有必要。您没有要展开的选项:audioEngine不是可选的,inputNode属性也不是可选的。如果让我们只使用unwrap选项。如果您想要创建一个新的名为node的常量,只需这样做:

if let node = audioEngine?.inputNode {
   node.removeTap(onBus: 0)
}