如何解决在Swift 2中音频录制器出现“表达式类型不明确”的问题,需要更多的上下文信息。

8

我已升级到Swift 2.0,当我尝试录音时遇到以下错误,但我并不太理解:

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

var recordSettings

我应该如何修复此错误,更重要的是为什么会出现这个错误?

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

    var dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    var docsDir: AnyObject = dirPaths[0]
    var soundFilePath = docsDir.stringByAppendingPathComponent("tempRecordzz")
    var soundFileURL:NSURL = NSURL(fileURLWithPath: soundFilePath)



    var error: NSError?
    do {
        recorder = try AVAudioRecorder(URL: soundFileURL, settings: recordSettings)
    } catch var error1 as NSError {
        error = error1
        recorder = nil
    }
1个回答

15
kAudioFormatAppleLossless这个类型在Swift 1.2/Xcode 6.4中是Int,而在Swift 2/Xcode 7中变成了Int32,并且在Swift 7.0.1中变成了UInt32。像Int32UInt32这样的固定大小的整数类型不会自动桥接到NSNumber对象以便插入NSDictionary中。

可以通过显式转换来解决此问题:

let recordSettings = [
    AVFormatIDKey: Int(kAudioFormatAppleLossless), // <-- HERE
    AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
    AVEncoderBitRateKey : 320000,
    AVNumberOfChannelsKey: 2,
    AVSampleRateKey : 44100.0
]

1
kAudioFormatAppleLossless 在 Xcode7.0.1 中是 UInt32 类型。 - rintaro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接