我建立了一个简单的记忆游戏,假设用户点击一张卡片,直到所有卡片都配对。但是,我的问题是,当用户点击两张卡时,游戏会在 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
}
}