ios Swift 2.0:没有更多上下文的表达式类型是不明确的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32663364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:42:34  来源:igfitidea点击:

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

iosswift2xcode7avfoundation

提问by lernerbot

The following used to work in Swift 1.2:

以下用于在 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".

“没有更多上下文,类型表达式是不明确的”。

采纳答案by Stephan

To comply to the required [String : AnyObject]format required by recordSettingsparameter; In addition to @Unheilig's answer, you'll need to convert your intsand floatsto NSNumber:

符合参数要求的[String : AnyObject]格式recordSettings;除了@Unheilig 的回答之外,您还需要将您的intsand转换floatsNSNumber

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
]

回答by Unheilig

You could give the compiler more information:

您可以为编译器提供更多信息:

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

回答by rghome

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

我还收到此错误消息,试图用 nil 初始化一组可选值:

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

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

表达式类型 'Array<Egg>' 在没有更多上下文的情况下是不明确的。

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

更改[Egg][Egg?]修复错误。