Swift “我怎样才能解决问题?”;线程1:致命错误:在展开可选值“0”时意外发现nil;迅速地
我正在尝试将音乐添加到我创建的游戏中。但我得到了一个错误: 线程1:致命错误:在展开可选值时意外发现nil 我在Stack Overflow()上找到了另一篇文章,但我不明白这是如何应用于我的代码的 这是我的代码:Swift “我怎样才能解决问题?”;线程1:致命错误:在展开可选值“0”时意外发现nil;迅速地,swift,avaudioplayer,optional,avaudiosession,Swift,Avaudioplayer,Optional,Avaudiosession,我正在尝试将音乐添加到我创建的游戏中。但我得到了一个错误: 线程1:致命错误:在展开可选值时意外发现nil 我在Stack Overflow()上找到了另一篇文章,但我不明白这是如何应用于我的代码的 这是我的代码: import UIKit import SpriteKit import GameplayKit import AVFoundation class GameViewController: UIViewController { var player:AVAudioPlaye
import UIKit
import SpriteKit
import GameplayKit
import AVFoundation
class GameViewController: UIViewController {
var player:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
do {
let audioPath = Bundle.main.path(forResource: "HomeSoundtrack", ofType: "m4a")
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
}
catch {
}
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
}
catch {
}
player.play()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
}
在您的代码中,您正在使用!强制展开一个可选项,如果强制展开的值恰好为零,则可能会导致此错误
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
这可能会导致错误,如果audioPath为零,则最好执行此操作
//If this line errors, there is a problem with finding HomeSoundtrack.m4a in the bundle
let audioPathURL: URL = Bundle.main.url(forResource: "HomeSoundtrack", withExtension: "m4a")!
do {
player = try AVAudioPlayer(contentsOf: audioPathURL)
} catch {
print("Couldn't load HomeSoundtrack file with error: \(error)")
}
或者
if let view = self.view as! SKView?
这个if-let应该看起来像
if let view: SKView = self.view as? SKView {
//Game stuff
}
可选值是可能包含
nil
的值,如果要获取其值,必须对其进行包装
但请使用安全包装,不要强制包装!
检查这条线
let audioPath = Bundle.main.path(forResource: "HomeSoundtrack", ofType: "m4a")
audioPath是一个可选
,因此它可能包含nil
值假设您写入HomeSoundtrack
错误或未找到文件,则audioPath将为零
然后强制包装!
它。在这一行中,如果音频路径为nil
,则它将崩溃
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
安全吗
let audioPathURL = Bundle.main.url(forResource: "HomeSoundtrack", withExtension: "m4a")
{
do {
player = try AVAudioPlayer(contentsOf: audioPathURL)
} catch {
print("Couldn't load HomeSoundtrack file")
}
}
首先找到何处/确定哪一行。这是调试中的一个重要步骤。浏览您链接的问题的答案。您首先需要找到导致错误的代码行。一旦您知道哪一行是问题所在,然后知道哪个变量是nil
,您就可以确定如何修复它。旁注-不要使用NSURL
在Swift中,使用URL
.1.从捆绑包加载路径不应该失败。应该强制展开路径。如果路径崩溃,则您知道自己犯了错误,例如忘记了定位文件。如果让获取路径,则无需使用路径。2.使用bundle.main.URL
直接获取URL。1.从捆绑包加载路径e不应该失败。它应该是强制展开的。如果它崩溃了,那么你就知道你犯了一个错误,比如忘记了定位文件。如果让
获取路径,就不需要。2.使用Bundle.main.url
直接获取url。抱歉@rmaddy我不理解这一行(1.从捆绑包加载路径不应该失败。它应该是强制展开的
),同时我更新我的回答在let audioPath…
行的末尾添加一个!
以强制展开可选路径。如果找不到文件,您希望它在开发过程中崩溃。是的,我最好在开发过程中崩溃