Error "fatal error: unexpectedly found nil while unwrapping an Optional value" when a segue is fired
Error "fatal error: unexpectedly found nil while unwrapping an Optional value" when a segue is fired
我在我的应用程序中添加了一个 segue,当我启动 segue 时,应用程序在 iPhone 5S 模拟器中崩溃(在其他模拟器中,它在此之前崩溃,因为特定于设备的代码。 是问题的 link),消息 "fatal error: unexpectedly found nil while unwrapping an Optional value" 被打印到控制台,错误 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
显示在初始化数组的行上。我认为原因是因为我有一个 UIImageView
s 的动画需要展开。这是我的做法:
// dieImages are UIImageViews that I am animating then displaying a random image of a die
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
// Die animation
for die in dieImages {
die.animationImages = [ <====== Error on this line
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die4")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die4")!
]
die.animationRepeatCount = 1
die.animationDuration = 1.0
}
但是 segue 是 AnyObject
类型,所以我认为这就是解包不起作用的原因:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}
如何安全地解包图像,或者在触发 segue 时根本不解包?我见过使用两个问号(但会导致 "could not find an overload for 'init' that accepts the supplied arguments" 错误)或 if 语句的方法,但我不知道如何实现。
更新
我试过这段代码,结果和以前一样
let image0 = UIImage(named: "dicey-die1")!
let image1 = UIImage(named: "dicey-die2")!
let image2 = UIImage(named: "dicey-die3")!
let image3 = UIImage(named: "dicey-die4")!
let image4 = UIImage(named: "dicey-die5")!
let image5 = UIImage(named: "dicey-die6")!
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
for die in dieImages {
die.animationImages = [
image1,
image5,
image0,
image3,
image2,
image4,
image2,
image0,
image5,
image2,
image4,
image1,
image2
]
die.animationRepeatCount = 1
die.animationDuration = 1.0
}
更新 2
我创建了一个包含所有图像的数组
let image0:[UIImage] = [ UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die4")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die3")!]
然后我在我的动画中使用了数组
for die in dieImages {
die.animationImages = [
image0
]
应用程序在 AppDelegate
处崩溃,所以我添加了一个异常断点,并且崩溃发生在动画数组上:
die.animationImages = [
并且这条消息被打印到控制台:
2015-09-08 07:23:05.192 Diced[80843:14927078] -[Swift._SwiftDeferredNSArray _isResizable]: unrecognized selector sent to instance 0x7fa738dd7460
最好的选择是图像不可用。
一个简单的测试,是否有效并实例化图像:
let image = UIImage(named: "dicey-die2")!
如果不是,您如何将图像添加到项目中?
我在我的应用程序中添加了一个 segue,当我启动 segue 时,应用程序在 iPhone 5S 模拟器中崩溃(在其他模拟器中,它在此之前崩溃,因为特定于设备的代码。Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
显示在初始化数组的行上。我认为原因是因为我有一个 UIImageView
s 的动画需要展开。这是我的做法:
// dieImages are UIImageViews that I am animating then displaying a random image of a die
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
// Die animation
for die in dieImages {
die.animationImages = [ <====== Error on this line
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die4")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die4")!
]
die.animationRepeatCount = 1
die.animationDuration = 1.0
}
但是 segue 是 AnyObject
类型,所以我认为这就是解包不起作用的原因:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}
如何安全地解包图像,或者在触发 segue 时根本不解包?我见过使用两个问号(但会导致 "could not find an overload for 'init' that accepts the supplied arguments" 错误)或 if 语句的方法,但我不知道如何实现。
更新
我试过这段代码,结果和以前一样
let image0 = UIImage(named: "dicey-die1")!
let image1 = UIImage(named: "dicey-die2")!
let image2 = UIImage(named: "dicey-die3")!
let image3 = UIImage(named: "dicey-die4")!
let image4 = UIImage(named: "dicey-die5")!
let image5 = UIImage(named: "dicey-die6")!
let dieImages = [dieImage0, dieImage1, dieImage2, dieImage3, dieImage4, dieImage5, dieImage6, dieImage7]
for die in dieImages {
die.animationImages = [
image1,
image5,
image0,
image3,
image2,
image4,
image2,
image0,
image5,
image2,
image4,
image1,
image2
]
die.animationRepeatCount = 1
die.animationDuration = 1.0
}
更新 2
我创建了一个包含所有图像的数组
let image0:[UIImage] = [ UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die4")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die1")!,
UIImage(named: "dicey-die6")!,
UIImage(named: "dicey-die3")!,
UIImage(named: "dicey-die5")!,
UIImage(named: "dicey-die2")!,
UIImage(named: "dicey-die3")!]
然后我在我的动画中使用了数组
for die in dieImages {
die.animationImages = [
image0
]
应用程序在 AppDelegate
处崩溃,所以我添加了一个异常断点,并且崩溃发生在动画数组上:
die.animationImages = [
并且这条消息被打印到控制台:
2015-09-08 07:23:05.192 Diced[80843:14927078] -[Swift._SwiftDeferredNSArray _isResizable]: unrecognized selector sent to instance 0x7fa738dd7460
最好的选择是图像不可用。
一个简单的测试,是否有效并实例化图像:
let image = UIImage(named: "dicey-die2")!
如果不是,您如何将图像添加到项目中?