以下是代码的一部分:
类AudioRecorderController:UIViewController,AVAudioRecorderDelegate {
var audioRecorder: AVAudioRecorder?
func audioRecordingPath() -> NSURL {
let fileManager = NSFileManager()
let documentsFolderUrl = fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false, error: nil)
return documentsFolderUrl!.URLByAppendingPathComponent("Recording.m4a")
}
func audioRecordingSettings() -> NSDictionary {...}
func startRecordingAudio() {
var error: NSError?
let audioRecordingURL = self.audioRecordingPath()
audioRecorder = AVAudioRecorder(URL: audioRecordingURL, settings: audioRecordingSettings() as [NSObject: AnyObject], error: &error)
if let recorder = audioRecorder {
recorder.delegate = self
if recorder.prepareToRecord() && recorder.record() {
println("Capture Succeed!")
let delayInSeconds = 5.0
let delayInNanoSeconds = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(delayInNanoSeconds, dispatch_get_main_queue(), {
[weak self] in
self!.audioRecorder!.stop()
//It breaks here.
})
}
}
}
它在控制台中打印“Capture succeed”,然后崩溃,错误“在展开Optional值时意外发现nil”。 最后一行代码有什么问题?
答案 0 :(得分:1)
您是否尝试过更换
self!.audioRecorder!.stop()
使用展开的常量
recorder.stop()