2015-06-18 90 views
1

我有這樣的功能:`NSDictionary`不能隱式轉換爲`[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. 
} 

它要求我以後添加爲[NSObject: AnyObject]audioRecordingSetthings(),我不認爲這是正確的解決方案。 因爲當我在另一個class中調用startRecordingAudio()時,它與Unexpectedly found nil崩潰。

+0

'kAudioFormatMPEG4AAC作爲NSNumber'應該是'的NSNumber(無符號整型:kAudioFormatMPEG4AAC)' ,因爲基本上不能直接將對象作爲對象進行鑄造。 – holex

回答

3

你只需要在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 
    ] 
} 
0

您需要檢查"AVAudioRecorder"初始化它規定:

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

所以,你需要轉換設置爲[NSObject : AnyObject]

相關問題