Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVF基金会播放声音迅捷2_Ios_Swift_Avfoundation - Fatal编程技术网

Ios AVF基金会播放声音迅捷2

Ios AVF基金会播放声音迅捷2,ios,swift,avfoundation,Ios,Swift,Avfoundation,我尝试用Swift 2.0播放声音 如果我写“试试”而不写我出错了 “不会处理从此处引发的错误” AVAudioPlayer不是可选的,为什么Xcode请求“try!” 如果我写‘试试!’我的应用程序崩溃 “在展开可选值时意外发现nil” 假装你从未见过在Swift中强制展开运算符,然后完全停止使用。基本上是“如果这个可选项包含nil,则崩溃”操作符。使用“if-let”样式可选绑定或try/catch,如@LLooggaann在其出色的回答中所述。(投票)假装你从未见过在Swift中强制展开运

我尝试用Swift 2.0播放声音 如果我写“试试”而不写我出错了 “不会处理从此处引发的错误” AVAudioPlayer不是可选的,为什么Xcode请求“try!” 如果我写‘试试!’我的应用程序崩溃 “在展开可选值时意外发现nil”


假装你从未见过
在Swift中强制展开运算符,然后完全停止使用。基本上是“如果这个可选项包含nil,则崩溃”操作符。使用“if-let”样式可选绑定或try/catch,如@LLooggaann在其出色的回答中所述。(投票)

假装你从未见过
在Swift中强制展开运算符,然后完全停止使用。基本上是“如果这个可选项包含nil,则崩溃”操作符。使用“if-let”样式可选绑定或try/catch,如@LLooggaann在其出色的回答中所述。(表决)

同意。我确实在
声程中使用了它,但这仅仅是因为我100%知道它在上面的行中是安全的,并且我假设它被声明为可选的是有原因的。如果让
超级安全,本可以添加一个额外的
。同意。我确实在
声程中使用了它,但这仅仅是因为我100%知道它在上面的行中是安全的,并且我假设它被声明为可选的是有原因的。如果让
超级安全的话,本可以添加一个额外的
class TouchViewController: UIViewController {
var soundPath:NSURL? 
...................
 //Play Bipsound
        do { soundPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Bipsound", ofType: "wav")!)
           var sound = try! AVAudioPlayer(contentsOfURL: soundPath!, fileTypeHint: nil)
            sound.prepareToPlay()
            sound.play() }
    var soundPath:NSURL? 

    if let path = Bundle.main.path(forResource: "Bipsound", ofType: "wav") {
        soundPath = NSURL(fileURLWithPath: path)
        do {
            let sound = try AVAudioPlayer(contentsOfURL: soundPath!, fileTypeHint:nil)
            sound.prepareToPlay()
            sound.play()
        } catch {
            //Handle the error
        }
    }