`NSDictionary` 不能隐式转换为 `[NSObject : AnyObject]`

`NSDictionary` is not implicitly convertible to `[NSObject : AnyObject]`

我有这个功能:

func audioRecordingSettings() -> NSDictionary {

        return [
            AVFormatIDKey : kAudioFormatMPEG4AAC as NSNumber,
            AVSampleRateKey : 16000.0 as NSNumber,
            AVNumberOfChannelsKey : 1 as NSNumber,
            AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue as NSNumber
        ]
    }

然后我又定义了一个:

func startRecordingAudio() {

    var error: NSError?

    let audioRecordingURL = self.audioRecordingPath()

    audioRecorder = AVAudioRecorder(URL: audioRecordingURL, settings: audioRecordingSettings(), error: &error)
    //Here the error comes. 
 }

它要求我在 audioRecordingSetthings() 之后添加 [NSObject: AnyObject],我认为这不是正确的解决方案。 因为当我在另一个 class 中调用 startRecordingAudio() 时,它因 Unexpectedly found nil 而崩溃。

您需要检查指定的 "AVAudioRecorder" 初始化器:

init!(URL url: NSURL!, settings: [NSObject : AnyObject]!, error outError: NSErrorPointer)

因此,您需要将设置转换为 [NSObject : AnyObject]

你只需要在 AVAudioRecorderDelegate 中添加 audioRecordingSettings() as [NSObject : AnyObject]:

AVAudioRecorder(URL: audioRecordingURL, settings: audioRecordingSettings() as [NSObject : AnyObject], error: &error)

此外,我会将您的方法更改为:

func audioRecordingSettings() -> [NSObject : AnyObject] {

    return [
        AVFormatIDKey : kAudioFormatMPEG4AAC,
        AVSampleRateKey : 16000.0,
        AVNumberOfChannelsKey : 1,
        AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue
    ]
}