我已经构建了一个简单的记忆游戏,用户可以点按一张卡片直到所有卡片都配对。但是,我的问题是,当用户点击两张牌时,游戏在2秒后崩溃“在打开时意外发现nil”。
我已经仔细检查了所有图像并确保它们正确连接但我仍然得到错误。我的代码如下:
@IBOutlet weak var frontImageView: UIImageView!
@IBOutlet weak var backImageView: UIImageView!
var card: Card?{
didSet {
guard let card = card else {return}
frontImageView.image = card.image
}
}
fileprivate(set) var shown: Bool = false
//Mark:- Card function properties
func showCard(_ show: Bool, animated: Bool){
frontImageView.isHidden = false
backImageView.isHidden = false //This is where i get the error.
shown = show
if animated {
if show{
UIView.transition(from: backImageView, to: frontImageView, duration: 0.3, options: .transitionFlipFromBottom, completion: { (finished: Bool) in
})
} else {
UIView.transition(from: frontImageView, to: backImageView, duration: 0.3, options: .transitionFlipFromBottom, completion: { (finished: Bool) in
})
}
} else {
if show{
bringSubview(toFront: frontImageView)
backImageView.isHidden = true
} else {
bringSubview(toFront: backImageView)
frontImageView.isHidden = true
}
}
答案 0 :(得分:0)
至少有一个网点是零。有很多原因,例如:您忘记将插座连接到xib / storyboard文件,在showCard
之前调用viewDidLoad
,您只是从代码创建视图,而不是从storyboard / xib文件实例化。
像@dylanthelion建议您应该调试代码并查看代码堆栈。您还应该在此处粘贴更多代码。
在代码中保留启用的异常断点: https://www.youtube.com/watch?v=AvnqBjzEKuk