Swift 2.0: Expression type ambiguous without additional context?

The following data is used to work in Swift 1.2:

var recordSettings = [ AVFormatIDKey: kAudioFormatMPEG4AAC, AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey: 2, AVSampleRateKey : 44100.0] 

Now it gives an error:

"A type expression is ambiguous without additional context."

+42
ios swift2 xcode7 avfoundation
Sep 19 '15 at 2:01
source share
3 answers

You can provide the compiler with additional information:

 let recordSettings : [String : Any] = [ AVFormatIDKey: kAudioFormatMPEG4AAC, AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue, AVEncoderBitRateKey : 320000, AVNumberOfChannelsKey: 2, AVSampleRateKey : 44100.0 ] 
+56
Sep 19 '15 at 3:05
source share

Conform to the required format [String : AnyObject] required by the recordSettings parameter; In addition to @Unheilig's answer, you will need to convert your ints and floats to NSNumber :

 let recordSettings : [String : AnyObject] = [ AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC), AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue as NSNumber, AVEncoderBitRateKey : 320000 as NSNumber, AVNumberOfChannelsKey: 2 as NSNumber, AVSampleRateKey : 44100.0 as NSNumber ] 
+35
Sep 21 '15 at 10:01
source share

I also received this error message trying to initialize an array of options with nil:

 var eggs : [Egg] = Array<Egg>(count: 10, repeatedValue: nil) 

The type of the expression 'Array <Egg>' is ambiguous without additional context.

Changing [Egg] to [Egg?] Fixed the bug.

+3
Jun 03 '16 at 18:23
source share



All Articles