Swift 2.0: Type of Expression is ambiguous without more context?

51,218

Solution 1

To comply to the required [String : AnyObject] format required by recordSettings parameter; In addition to @Unheilig's answer, you'll 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
]

Solution 2

You could give the compiler more information:

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

Solution 3

I also got this error message trying to initialise an array of optionals with nil:

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

Expression Type 'Array<Egg>' is ambiguous without more context.

Changing [Egg] to [Egg?] fixed the error.

Share:
51,218
lernerbot
Author by

lernerbot

Currently developing for iOS

Updated on July 09, 2022

Comments

  • lernerbot
    lernerbot almost 2 years

    The following 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 the error:

    "Type expression is ambiguous without more context".

  • lernerbot
    lernerbot over 8 years
    Thank you for this. I didn't Know Any was available. I was trying AnyObject and that didn't work. Sadly the receiver of recordSettings requires [String : AnyObject]
  • Stephan
    Stephan over 8 years
    @lernerbot, see my answer below.
  • lernerbot
    lernerbot over 8 years
    I agree except I didn't ask the question correctly. @Stephan answered the question I meant to ask.
  • ZAFAR007
    ZAFAR007 almost 8 years
    Hi Unheilig, Can you please check my question i have same issue? Thanks http://stackoverflow.com/questions/38279942/type-of-expressi‌​on-is-ambiguous-with‌​out-more-context-swi‌​ft-2-0