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

2.8k views Asked by At

I have this function:

func audioRecordingSettings() -> NSDictionary {

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

Then I defined another:

func startRecordingAudio() {

    var error: NSError?

    let audioRecordingURL = self.audioRecordingPath()

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

It asks me to add as [NSObject: AnyObject] after the audioRecordingSetthings(), I don't think that's the correct solution. Because when I called startRecordingAudio() in another class, it crashes with Unexpectedly found nil.

2

There are 2 answers

0
Teo On BEST ANSWER

You just need to add audioRecordingSettings() as [NSObject : AnyObject] in the AVAudioRecorderDelegate:

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

Additionally I would change your method to:

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

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

You need to check the "AVAudioRecorder" initializer which specifies:

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

So, you need to convert setting to [NSObject : AnyObject].