【问题标题】:Swift 2.0: Type of Expression is ambiguous without more context?Swift 2.0:没有更多上下文的表达式类型是模棱两可的?
【发布时间】:2015-12-16 06:32:44
【问题描述】:

以下内容曾经在 Swift 1.2 中工作:

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

现在,它给出了错误:

“类型表达式在没有更多上下文的情况下是模棱两可的”。

【问题讨论】:

    标签: ios swift swift2 avfoundation xcode7


    【解决方案1】:

    你可以给编译器更多的信息:

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

    【讨论】:

    • 谢谢你。我不知道任何可用。我正在尝试 AnyObject,但没有奏效。可悲的是,recordSettings 的接收者需要 [String : AnyObject]
    • @lernerbot,请看下面我的回答。
    • 嗨Unheilig,你能检查我的问题我有同样的问题吗?谢谢http://stackoverflow.com/questions/38279942/type-of-expression-is-ambiguous-without-more-context-swift-2-0
    【解决方案2】:

    符合recordSettings参数要求的[String : AnyObject]格式;除了@Unheilig 的回答,您还需要将您的intsfloats 转换为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
    ]
    

    【讨论】:

    • 我同意,但我没有正确提出问题。 @Stephan 回答了我想问的问题。
    【解决方案3】:

    我在尝试使用 nil 初始化一个可选数组时也收到此错误消息:

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

    表达式类型 'Array' 在没有更多上下文的情况下是模棱两可的。

    [Egg] 更改为[Egg?] 修复了该错误。

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2016-12-10
      • 2018-02-15
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多