swift 2 dictionary is ambigious without more context

时间:2015-09-01 22:34:09

标签: xcode swift

in converting to swift 2 from 1.2 i'm getting an error, type of expression is ambiguous without more context.

        var recordSettings = [
        AVFormatIDKey: kAudioFormatLinearPCM,
        AVSampleRateKey : 44100.0,
        AVNumberOfChannelsKey: 1,
        AVLinearPCMBitDepthKey : 32,
        AVLinearPCMIsFloatKey : true

I have no idea what the problem is . I've tried casting to [String:AnyObject] but with no luck. any suggestions are appreciated.

1 个答案:

答案 0 :(得分:2)

The type inferencer is having trouble realizing that kAudioFormatLinearPCM is an alias for UInt32 and that that can be boxed in an NSNumber.

So help it:

var recordSettings:[String:AnyObject] = [
  AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatLinearPCM),
  AVSampleRateKey : 44100.0,
  AVNumberOfChannelsKey: 1,
  AVLinearPCMBitDepthKey : 32,
  AVLinearPCMIsFloatKey : true]