1

I am trying with learning Swift and got stuck on an issue here.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var diceImageView1: UIImageView!

@IBOutlet weak var diceImageView2: UIImageView!

var leftDiceNumber=1

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Do any additional setup after loading the view.

    diceImageView1.image = UIImage(imageLiteralResourceName: "DiceSix")

    // change transparency with diceImageView1.alpha=0.7

        }

@IBAction func rollButtonPressed(_ sender: UIButton) {
    print("button pressed")
    diceImageView1 = [ UIImageView(imageLiteralResourceName: "DiceOne"),UIImageView(imageLiteralResourceName: "DiceTwo"),UIImageView(imageLiteralResourceName: "DiceThree"),UIImageView(imageLiteralResourceName: "DiceFour"),UIImageView(imageLiteralResourceName: "DiceFive"),UIImageView(imageLiteralResourceName: "DiceSix")],[leftDiceNumber]
    
    leftDiceNumber=leftDiceNumber+1
  
}

}

But all I get is the error messages on the IBAction:

1.Argument passed to call that takes no arguments

2.Cannot assign value of type '[UIImageView]' to type 'UIImageView'

3.Consecutive statements on a line must be separated by ';'

4.Expected expression

What's the difference between UIImageView and UIImage ? When they should be used?

Many Thanks in advance !

2
  • A UIImageView displays a UIImage. Your code is trying to assign an array of UIImageViews to a UIImageView variable. What are you actually trying to do? Commented Jun 9, 2022 at 19:44
  • I have 6 images of dices. Once I press the button, it should change from dice 1 to 2, from 2 to 3, etc (n+1) Commented Jun 9, 2022 at 20:20

1 Answer 1

2

You want to change the .image property to change the image.

To "cycle through" the dice, you could do this:

class DiceViewController: UIViewController {
    
    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!

    let diceNames: [String] = [
        "DiceOne", "DiceTwo", "DiceThree", "DiceFour", "DiceFive", "DiceSix"
    ]
    
    var leftDiceNumber = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        diceImageView1.image = UIImage(named: diceNames[leftDiceNumber % 6])
    }
    
    @IBAction func rollButtonPressed(_ sender: UIButton) {
        print("button pressed")

        // increment the index
        leftDiceNumber += 1
        
        // udpate the image view
        diceImageView1.image = UIImage(named: diceNames[leftDiceNumber % 6])

    }
    
}

I'm guessing your goal is to "randomly roll" the dice, so take a look at this slightly different class:

class DiceViewController: UIViewController {
    
    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!
    
    let diceNames: [String] = [
        "DiceOne", "DiceTwo", "DiceThree", "DiceFour", "DiceFive", "DiceSix"
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // start with both dice at One
        diceImageView1.image = UIImage(named: diceNames[0])
        diceImageView2.image = UIImage(named: diceNames[0])

    }
    
    @IBAction func rollButtonPressed(_ sender: UIButton) {
        print("button pressed")
        
        // arrays are Zero-based, so get a random Int
        //  from 0 to 5
        //let l = Int.random(in: 0...5)
        //let r = Int.random(in: 0...5)
        //diceImageView1.image = UIImage(named: diceNames[l])
        //diceImageView2.image = UIImage(named: diceNames[r])

        // more "modern Swifty" method
        if let nm = diceNames.randomElement() {
            diceImageView1.image = UIImage(named: nm)
        }
        if let nm = diceNames.randomElement() {
            diceImageView2.image = UIImage(named: nm)
        }

    }
    
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not simply diceNames.randomElement()?
@LeoDabus - whoops... clear evidence that I'm not a Swift developer :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.