【问题标题】:`NSDictionary` is not implicitly convertible to `[NSObject : AnyObject]``NSDictionary` 不能隐式转换为 `[NSObject : AnyObject]`
【发布时间】:2015-06-18 13:38:38
【问题描述】:

我有这个功能:

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 一起崩溃。

【问题讨论】:

  • kAudioFormatMPEG4AAC as NSNumber 应该是NSNumber(unsignedInt: kAudioFormatMPEG4AAC),因为基本上不能将原语直接转换为对象。

标签: ios swift nsdictionary multimedia


【解决方案1】:

您只需在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
    ]
}

【讨论】:

    【解决方案2】:

    您需要检查 "AVAudioRecorder" 初始化程序,它指定:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多