How to resolve Type of expression is ambiguous without additional context for audio recordings in swift 2

I upgraded to Swift 2.0, and I completely don’t understand this when I try to record sound:

The type of expression is ambiguous without additional context.

on var recordSettings

What should I do to fix this error and more importantly why?

  var recordSettings = [ AVFormatIDKey: kAudioFormatAppleLossless, AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey: 2, AVSampleRateKey : 44100.0 ] var dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) var docsDir: AnyObject = dirPaths[0] var soundFilePath = docsDir.stringByAppendingPathComponent("tempRecordzz") var soundFileURL:NSURL = NSURL(fileURLWithPath: soundFilePath) var error: NSError? do { recorder = try AVAudioRecorder(URL: soundFileURL, settings: recordSettings) } catch var error1 as NSError { error = error1 recorder = nil } 
+8
ios ios9 swift swift2
source share
1 answer

The kAudioFormatAppleLossless type kAudioFormatAppleLossless changed from Int (Swift 1.2 / Xcode 6.4) to Int32 (Swift 2 / Xcode 7) and UInt32 in Swift 7.0.1. Fixed-size integers such as Int32 and UInt32 do not automatically connect to NSNumber objects for insertion into an NSDictionary .

Explicit conversion helps solve the problem:

 let recordSettings = [ AVFormatIDKey: Int(kAudioFormatAppleLossless), // <-- HERE AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey: 2, AVSampleRateKey : 44100.0 ] 
+15
source share

All Articles