(SWIFT)初始值设定条件的结合必须有可选的类型,而不是“AVAudioInputNode”
问题描述:
我想创建一个语音到文本的功能,我得到的错误:(SWIFT)初始值设定条件的结合必须有可选的类型,而不是“AVAudioInputNode”
Initializer for conditional binding must have Optional type, not 'AVAudioInputNode'
guard let inputNode = audioEngine.inputNode else {
fatalError("Audio engine has no input node")
}
答
AVAudioEngine
“ s inputNode
属性不是可选的。当第一次访问inputNode时,音频引擎按需创建单例。它不可能是零,因为这个守卫没有意义。
因此,只需移除警卫并按原样使用audioEngine.inputNode
即可。它不能是nil
。
你仍然需要确保该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.
(从苹果公司的文档)
我有完全相反的问题,在我的情况下'inputNode'即使文档中另有说明,也是可选的。 – shelll
你是如何实现的,它不是零?你使用Swift 4吗?你链接到你的项目的哪个“AV”框架? – shelll
这是Apple的示例代码,它看起来不起作用 – user798719