问题“尝试?”和AVAudioPlayer [英] Issue with "try?" and AVAudioPlayer

查看:68
本文介绍了问题“尝试?”和AVAudioPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到AVAudioPlayer的以下问题:

I am having the following issue with AVAudioPlayer:

import Foundation   //Needed for dispatch_once_t
import AVFoundation //Needed to play sounds

class PlayStartSong {

    var song: AVAudioPlayer = AVAudioPlayer()
    var songStarted: Bool = false

    class var sharedInstance: PlayStartSong {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: PlayStartSong? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = PlayStartSong()
        }
        return Static.instance!
    }

    func prepareAudios() {

        var path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
        song = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))    //Error Here
        song.prepareToPlay()

        song.numberOfLoops = -1 //Makes the song play repeatedly
    }
}

在分配变量song的值的行上prepareAudios函数,转换为Swift 2.0后出现以下错误:

On the line that assigns the value of the variable "song" in the "prepareAudios" function, I am getting the following error after converting to Swift 2.0:


可选类型'AVAudioPLayer?'的值未解包;你的意思是使用'!'还是'?'?

Value of optional type 'AVAudioPLayer?' not unwrapped; did you mean to use '!' or '?'?

然而,在使用建议的修补程序时,它告诉我删除我刚刚补充的感叹号。这里的确切问题是什么?

However, upon using the suggested fix, it is telling me to remove the exclamation point I just added. What is the exact issue here?

推荐答案

要使用试试? as你想要的歌曲变量必须是一个可选:

To use try? as you intended your song variable has to be an Optional:

class PlayStartSong {

    var song: AVAudioPlayer?
    var songStarted: Bool = false

    class var sharedInstance: PlayStartSong {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: PlayStartSong? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = PlayStartSong()
        }
        return Static.instance!
    }

    func prepareAudios() {
        let path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
        song = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
        song?.prepareToPlay()
        song?.numberOfLoops = -1 //Makes the song play repeatedly
    }
}

要不使它成为可选项,你可以使用试试内捕捉

To not make it an Optional, you would use try inside do catch:

func prepareAudios() {
    do {
        let path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
        song = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
        song.prepareToPlay()
        song.numberOfLoops = -1 //Makes the song play repeatedly
    } catch {
        print(error)
    }
}

此外,如果您绝对确定创建AVAudioPlayer实例将永远成功,您可以通过使用来忽略抓住尝试!

Also, if you are absolutely sure that creating the AVAudioPlayer instance will always succeed, you can ignore "do catch" by using try!:

func prepareAudios() {
    let path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
    song = try! AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
    song.prepareToPlay()
    song.numberOfLoops = -1 //Makes the song play repeatedly
}

这篇关于问题“尝试?”和AVAudioPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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