如何解决表达式类型不明确,而Swift 2中的录音机没有更多上下文 [英] How to resolve Type of expression is ambiguous without more context for an audio recorder in swift 2

查看:101
本文介绍了如何解决表达式类型不明确,而Swift 2中的录音机没有更多上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经升级到Swift 2.0,当我尝试录制声音时我还是不太明白:

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

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
    }

推荐答案

kAudioFormatAppleLossless的类型从Int(Swift 1.2/Xcode 6.4)更改为Int32(Swift 2/Xcode 7)和UInt32在Swift 7.0.1中. 固定大小整数类型 像Int32UInt32一样 not 不会自动桥接到NSNumber对象 用于插入NSDictionary.

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
]

这篇关于如何解决表达式类型不明确,而Swift 2中的录音机没有更多上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆