播放音频文件时找不到 'PathForResource' 的过载

Could not find overload for 'PathForResource' when playing audio file

我有一个 Swift 文件在 Xcode6 中运行良好。当我升级到 Xcode7 时,我现在收到阻止程序编译的错误。下面是不起作用的代码片段。我收到的错误只是指出 "Could not find overload for 'PathForResource' that accepts the supplied arguments."

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 现在 returns NSURL 而不是 NSURL?

具体来说,更改为:

sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("red", ofType: "wav")!)!

为此:

sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("red", ofType: "wav")!)

…问题应该消失了。