xcode (Swift) 条件绑定的初始值设定项必须具有 Optional 类型,而不是“AVAudioInputNode”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46392377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:17:40  来源:igfitidea点击:

(Swift) Initializer for conditional binding must have Optional type, not 'AVAudioInputNode'

xcodeconditionaloptionalspeechavaudioengine

提问by Niall Kiddle

I am trying to create a speech to text function and I am getting the error:

我正在尝试为文本功能创建语音,但出现错误:

Initializer for conditional binding must have Optional type, not 'AVAudioInputNode'

Initializer for conditional binding must have Optional type, not 'AVAudioInputNode'

guard let inputNode = audioEngine.inputNode else {
        fatalError("Audio engine has no input node")
    }

采纳答案by joern

AVAudioEngine's inputNodeproperty is not an optional. The Audio Engine creates a singleton on demand when inputNode is first accessed. It cannot be nil and because of that the guard does not make sense.

AVAudioEngineinputNode属性不是可选的。当第一次访问 inputNode 时,音频引擎会根据需要创建一个单例。它不能为零,因此守卫没有意义。

So, just remove the guard and use audioEngine.inputNodeas it is. It cannot be nil.

因此,只需取下防护装置并按audioEngine.inputNode原样使用即可。它不能nil

You still have to make sure that the inputNodeis connected to something before using it:

inputNode在使用它之前,您仍然必须确保它已连接到某些东西:

Check the input format of input node (specifically, the hardware format) for a non-zero sample rate and channel count to see if input is enabled.

检查输入节点的输入格式(特别是硬件格式)是否为非零采样率和通道数,以查看是否启用了输入。

(from Apple's Documentation)

(来自苹果的文档)