我有一个可以在Xcode6中正常工作的Swift文件。当我升级到Xcode7时,现在出现错误,阻止程序编译。以下是无效的代码段。我收到的错误只是指出“找不到接受提供的参数的'PathForResource'重载”。
func playRecord(sender:UITapGestureRecognizer){
var tag = sender.view!.tag
let sound:NSURL
switch tag{
case 0: // red
sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("red", ofType: "wav")!)!
case 1: // blue
sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("blue", ofType: "wav")!)!
case 2: // white
sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("white", ofType: "wav")!)!
default:
sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("red", ofType: "wav")!)!
}
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: sound)
} catch _ {
audioPlayer = nil
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
Swift 2的感叹号太多,因为fileURLWithPath
现在返回NSURL
not NSURL?
。
具体来说,更改此:
sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("red", ofType: "wav")!)!
对此:
sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("red", ofType: "wav")!)
…问题应该解决。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句