I’m using Xcode Version 12.4 (12D4e) and I would like to display an image instead of printing. My code so far is this:
class ViewController: UIViewController {
@IBOutlet weak var diceImageView1: UIImageView!
@IBOutlet weak var diceImageView2: UIImageView!
@IBOutlet weak var combinedLabel: UILabel!
let diceArray = [#imageLiteral(resourceName: "DiceOne"),#imageLiteral(resourceName: "DiceTwo"), #imageLiteral(resourceName: "DiceThree"), #imageLiteral(resourceName: "DiceFour"), #imageLiteral(resourceName: "DiceFive"), #imageLiteral(resourceName: "DiceSix")]
@IBAction func rollButtonPressed(_ sender: UIButton) {
let x = Int.random(in: 1...6)
let y = Int.random(in: 1...6)
let z = x + y
diceImageView1.image = diceArray[x-1] // dice start counting at 1
diceImageView2.image = diceArray[y-1] // array indices start at 0
combinedLabel.text = "sum: (z)"
if z >= 9 && x < 9{
print("you win")
} else {
print("you died")
}
}
}
I’ve tried the code and it worked but instead of printing the result, I would like to display an image instead.
New contributor
Ligia Peres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.