以下曾经在Swift 1.2中工作:
var recordSettings = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0]
现在,它给出了错误:
“类型表达式在没有更多上下文的情况下是模棱两可的”。
您可以为编译器提供更多信息:
let recordSettings : [String : Any] =
[
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
]
遵守recordSettings
参数所需的[String:AnyObject]
格式;除了@Unheilig的答案,您还需要将int
和floats
转换为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?]
修复了错误。