How can I fix "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" in Swift
How can I fix "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" in Swift
我正在尝试将音乐添加到我创建的游戏中。但我收到错误:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
我在 Stack Overflow () 上发现了另一个 post,但我不明白这如何适用于我的代码。
这是我的代码:
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 为 nil,这可能会导致错误,最好这样做
//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 是一个 Optional
所以它可能包含 nil
值假设你写 HomeSoundtrack
错误或找不到文件那么 audioPath 将为 nil
然后你强制包装 !
它。在这一行中,如果 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")
}
}
我正在尝试将音乐添加到我创建的游戏中。但我收到错误:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
我在 Stack Overflow (
这是我的代码:
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 为 nil,这可能会导致错误,最好这样做
//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 是一个 Optional
所以它可能包含 nil
值假设你写 HomeSoundtrack
错误或找不到文件那么 audioPath 将为 nil
然后你强制包装 !
它。在这一行中,如果 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")
}
}