有3種方法可以用來解決此問題。
- 使用try創建可選的AVAudioRecorder?
- 如果你知道它會返回你AVRecorder,你可以默示使用試試吧!
- 或者然後使用try/catch語句
使用嘗試處理錯誤?
// notice that it returns AVAudioRecorder?
if let recorder = try? AVAudioRecorder(URL: soundFileURL, settings: recordSettings) {
// your code here to use the recorder
}
使用try!
// this is implicitly unwrapped and can crash if there is problem with soundFileURL or recordSettings
let recorder = try! AVAudioRecorder(URL: soundFileURL, settings: recordSettings)
的try/catch
// 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)")
}