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

qkf9rpyu  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(220)

以下内容在Swift 1.2中使用:

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

现在,它给出了错误:
没有更多上下文,类型表达式不明确。

inkz8wg9

inkz8wg91#

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

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

ma8fv8wu2#

要符合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
]
jutyujz0

jutyujz03#

我还收到了这个错误消息,试图初始化一个数组的可选与nil:

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

表达式类型“Array”在没有更多上下文的情况下不明确。
[Egg]更改为[Egg?]修复了该错误。

相关问题