Swift 中未分配 AVAudioRecorder 委托

AVAudioRecorder delegate not assigned in Swift

我决定将录音机 class 从 Objective C 重写为 Swift。 在 Objective C 中录音有效,但在 Swift AVAudioRecorderDelegate 中未调用委托方法,录音机成功启动。

我该如何解决这个问题?

class VoiceRecorder: NSObject, AVAudioPlayerDelegate, AVAudioRecorderDelegate {
    var audioRecorder: AVAudioRecorder!
    var audioPlayer: AVAudioPlayer?

    override init() {
        super.init()
        var error: NSError?

        let audioRecordingURL = self.audioRecordingPath()
        audioRecorder = AVAudioRecorder(URL: audioRecordingURL,
            settings: self.audioRecordingSettings(),
            error: &error)

        audioRecorder.meteringEnabled = true
        /* Prepare the recorder and then start the recording */
        audioRecorder.delegate = self
        if audioRecorder.prepareToRecord(){
            println("Successfully prepared for record.")
        }
    }

    func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
        println("stop")
        if flag{
            println("Successfully stopped the audio recording process")
            if completionHandler != nil {
                completionHandler(success: flag)
            }
        } else {
            println("Stopping the audio recording failed")
        }
    }
}

func record(){
    audioRecorder.record()
}

//UPD.
func stop(#completion:StopCompletionHandler){
    self.completionHandler = completion
    self.audioRecorder?.stop
}

问题出在这一行:

self.audioRecorder?.stop

这不是对 stop 方法的调用。它只是提到了方法的名称。你想这样说:

self.audioRecorder?.stop()

那些括号让一切变得不同。