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

ler*_*bot 42 avfoundation ios xcode7 swift2

以下用于在Swift 1.2中工作:

var recordSettings = [
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
    AVEncoderBitRateKey : 320000,
    AVNumberOfChannelsKey: 2,
    AVSampleRateKey : 44100.0]
Run Code Online (Sandbox Code Playgroud)

现在,它给出了错误:

"没有更多的上下文,类型表达式是模糊的".

Unh*_*lig 57

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

let recordSettings : [String : Any] =
[
    AVFormatIDKey: kAudioFormatMPEG4AAC,
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
    AVEncoderBitRateKey : 320000,
    AVNumberOfChannelsKey: 2,
    AVSampleRateKey : 44100.0
]
Run Code Online (Sandbox Code Playgroud)


Ste*_*han 35

符合参数要求的[String : AnyObject]格式recordSettings; 除了@ Unheilig的答案,你需要你的转换intsfloatsNSNumber:

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
]
Run Code Online (Sandbox Code Playgroud)

  • 我同意,除非我没有正确地提出这个问题.@Stephan回答了我想问的问题. (2认同)