因此这段代码在 iOS 模拟器上运行良好,但在我的 iPad Mini 上运行不正常
var sound = NSURL(fileURLWithPath:"/Users/Dan/Documents/XCode Code/Colors- Tabbed?/Colors- Tabbed?/Sweg.aiff")
var audioPlayer = AVAudioPlayer()
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: &error)
var error: NSError?
我在最后一行收到错误“unexpectedly found nil while unwrapping an Optional value”。
最佳答案
看起来 AVAudioPlayer
还没有被审计。它返回一个隐式解包的可选值,它可以是 nil
,显然 是 在你的 iPad 上。 (可能是因为您的 iPad 不知道 /Users/Dan/Documents/...
在哪里,因为它在您的计算机上。)
你想在一个可选值中捕获播放器,这样你就可以在使用它之前测试 nil
:
var sound = NSURL(fileURLWithPath:"/Users/Dan/Documents/XCode Code/Colors- Tabbed?/Colors- Tabbed?/Sweg.aiff")
var error: NSError?
var audioPlayer: AVAudioPlayer? = AVAudioPlayer(contentsOfURL: sound, error: &error)
if let audioPlayer = audioPlayer {
// do things with the audioPlayer
}
关于swift - 使用 AVAudioPlayer 解包 Optional 值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094651/