How to resolve Type of expression is ambiguous without more context for an audio recorder in swift 2

不想你离开。 提交于 2019-12-10 03:19:10

问题


I have upgraded to Swift 2.0 and I quite can't understand this when I try to record a sound:

Type of expression is ambiguous without more context

on var recordSettings

What should I do to fixt this error and more important, why?

 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:


The type of kAudioFormatAppleLossless changed from Int (Swift 1.2/Xcode 6.4) to Int32 (Swift 2/Xcode 7) and UInt32 in Swift 7.0.1. The fixed sized integer types like Int32 and UInt32 are not automatically bridged to NSNumber objects for insertion in an NSDictionary.

An explicit conversion helps to resolve the issue:

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


来源:https://stackoverflow.com/questions/32509565/how-to-resolve-type-of-expression-is-ambiguous-without-more-context-for-an-audio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!