我在我的应用程序中添加了一个 segue,当我触发 segue 时,应用程序在 iPhone 5S 模拟器中崩溃(在其他模拟器中,由于设备特定的代码,它在此之前崩溃。这是问题的链接),消息“致命错误:在展开可选值时意外发现 nil”被打印到控制台,并且错误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
,所以我认为这就是 unwrapping 不起作用的原因:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}
如何安全地解开图像,或者当 segue 被触发时根本不解开?我已经看到使用两个问号的方法(但这会导致“找不到接受提供的参数的 'init' 的重载”的错误)或 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