简体   繁体   中英

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

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 .

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
    ]
}

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM