Swift 2.0:没有更多上下文,表达式类型不明确?
Swift 2.0: Type of Expression is ambiguous without more context?
以下用于 Swift 1.2:
var recordSettings = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0]
现在,它给出错误:
"Type expression is ambiguous without more context".
你可以给编译器更多的信息:
let recordSettings : [String : Any] =
[
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
]
遵守recordSettings
参数要求的[String : AnyObject]
格式;除了@Unheilig 的回答之外,您还需要将 ints
和 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)
Expression Type 'Array<Egg>' is ambiguous without more context.
将 [Egg]
更改为 [Egg?]
修复了错误。
以下用于 Swift 1.2:
var recordSettings = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0]
现在,它给出错误:
"Type expression is ambiguous without more context".
你可以给编译器更多的信息:
let recordSettings : [String : Any] =
[
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
]
遵守recordSettings
参数要求的[String : AnyObject]
格式;除了@Unheilig 的回答之外,您还需要将 ints
和 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)
Expression Type 'Array<Egg>' is ambiguous without more context.
将 [Egg]
更改为 [Egg?]
修复了错误。