您可以使用 3 种方法来解决此问题。
- 使用 try 创建可选的 AVAudioRecorder?
- 如果你知道它会返回 AVRecorder,你可以隐式使用 try!
- 或者然后使用 try / catch 处理错误
使用尝试?
// notice that it returns AVAudioRecorder?
if let recorder = try? AVAudioRecorder(URL: soundFileURL, settings: recordSettings) {
// your code here to use the recorder
}
使用尝试!
// this is implicitly unwrapped and can crash if there is problem with soundFileURL or recordSettings
let recorder = try! AVAudioRecorder(URL: soundFileURL, settings: recordSettings)
试着抓
// The best way to do is to handle the error gracefully using try / catch
do {
let recorder = try AVAudioRecorder(URL: soundFileURL, settings: recordSettings)
} catch {
print("Error occurred \(error)")
}