Swift &引用;Thread1:致命错误:在展开可选值“0”时意外发现零;
这些是我使用的wav文件 我附上了一个屏幕截图,它是抛出这个错误 这就是我现在拥有的。我以为我一切都对。我知道这与使用可选的方法有关,但不确定如何修复它Swift &引用;Thread1:致命错误:在展开可选值“0”时意外发现零;,swift,xcode,optional,Swift,Xcode,Optional,这些是我使用的wav文件 我附上了一个屏幕截图,它是抛出这个错误 这就是我现在拥有的。我以为我一切都对。我知道这与使用可选的方法有关,但不确定如何修复它 import UIKit import AVFoundation class SecondViewController: UIViewController { var audioPlayer: AVAudioPlayer? override func viewDidLoad() { super.viewDid
import UIKit
import AVFoundation
class SecondViewController: UIViewController
{
var audioPlayer: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
do
{
let catSound = Bundle.main.path(forResource: "Cat", ofType: "wav")
let horseSound = Bundle.main.path(forResource: "Horse", ofType: "wav")
let dogSound = Bundle.main.path(forResource: "Dog", ofType: "wav")
let raccoonSound = Bundle.main.path(forResource: "Raccoon", ofType: "wav")
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: catSound!))
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: horseSound!))
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: dogSound!))
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: raccoonSound!))
}
catch
{
print(error)///
}
}
@IBAction func cat(_ sender: Any)
{
audioPlayer?.play()
}
@IBAction func horse(_ sender: Any)
{
audioPlayer?.play()
}
@IBAction func dog(_ sender: Any)
{
audioPlayer?.play()
}
@IBAction func raccoon(_ sender: Any){
audioPlayer?.play()
}
}
你的一个资源是零。 在(强制)展开之前,应检查您的分辨率是否为零:
if let catSound = Bundle.main.path(forResource: "Cat", ofType: "wav")
{
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: catSound))
}
对每个资源执行此操作,并使用调试器查看哪一个是nil。正如@Chris和@inexcitus所提到的,您的完整代码如下所示
class SecondViewController: UIViewController {
var audioPlayer: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func cat(_ sender: Any)
{
if let catSound = Bundle.main.path(forResource: "cat", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: catSound))
audioPlayer?.play()
}else {
print("Cat File is missing")
}
}
@IBAction func horse(_ sender: Any)
{
if let horseSound = Bundle.main.path(forResource: "horse", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: horseSound))
audioPlayer?.play()
}else {
print("Horse File is missing")
}
}
@IBAction func dog(_ sender: Any)
{
if let dogSound = Bundle.main.path(forResource: "dog", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: dogSound))
audioPlayer?.play()
}else {
print("Dog File is missing")
}
}
@IBAction func raccoon(_ sender: Any)
{
if let raccoonSound = Bundle.main.path(forResource: "raccoon", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: raccoonSound))
audioPlayer?.play()
}else {
print("Raccoon File is missing")
}
}}
区分大小写很重要。 文件是小写的(“cat”),但代码中的参数字符串是大写的(“cat”)。参数字符串和文件名必须完全匹配 您的代码无论如何都不是很有用,因为它在
viewDidLoad
中覆盖音频播放器实例三次
一个更有效的解决方案是创建一种方法来加载文件和播放声音,并使用Bundle
的URL相关API
强制展开很好,因为所有文件都必须在编译时在包中。可以立即修复设计错误(文件丢失或名称拼写错误)
class SecondViewController: UIViewController
{
var audioPlayer: AVAudioPlayer!
@IBAction func cat(_ sender: Any) {
playSound(withName: "cat")
}
@IBAction func horse(_ sender: Any) {
playSound(withName: "horse")
}
@IBAction func dog(_ sender: Any) {
playSound(withName: "dog")
}
@IBAction func raccoon(_ sender: Any){
playSound(withName: "raccoon")
}
func playSound(withName name : String) {
let sound = Bundle.main.url(forResource: name, withExtension: "wav")!
audioPlayer = try! AVAudioPlayer(contentsOf: sound)
audioPlayer.play()
}
}
当您遇到该错误时,请查找感叹号(
!
),这意味着一个可选项正在强制展开。我认为你的声音是错误的。另外,您为每个声音设置了相同的音频播放器实例,因此只有最后一个声音会保留。尝试在每个按钮动作中设置声音。这是否回答了您的问题?如果可能的话,请您共享崩溃行的屏幕截图。将URL(fileURLWithPath:catSound!)
替换为可选,而不是强制展开<代码>URL(fileURLWithPath:catSound???)。若强制展开的值为零,则强制展开将崩溃。您还可以在Xcode中共享音频文件的屏幕截图吗