Call can throw, but errors can not be thrown out of a global variable initializer

后端 未结 2 1229
再見小時候
再見小時候 2021-02-13 19:59

I\'m using Xcode 7 beta and after migrating to Swift 2 I experienced some issues with this line of code:

let recorder = AVAudioRecorder(URL: soundFileURL, settin         


        
2条回答
  •  青春惊慌失措
    2021-02-13 20:40

    If you know that your function call will not throw an exception, you can call the throwing function with try! to disable error propagation. Note that this will throw a runtime exception if an error is actually thrown.

    let recorder = try! AVAudioRecorder(URL: soundFileURL, settings: recordSettings as! [String : AnyObject])
    

    Source: Apple Error Handling documentation (Disabling Error Propagation)

提交回复
热议问题