提问者:小点点

Swift 2.0:表达的类型在没有更多上下文的情况下是模棱两可的?


以下曾经在Swift 1.2中工作:

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

现在,它给出了错误:

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


共3个答案

匿名用户

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

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

匿名用户

遵守recordSettings参数所需的[String:AnyObject]格式;除了@Unheilig的答案,您还需要将intfloats转换为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
]

匿名用户

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

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

“表达式类型”数组

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