4

I am trying to run this code that will randomise a UIImage. But I am getting the following error Cannot call value of non-function type 'UIImage?

@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var PersonsChoice: UIImageView!
var option = ["rock.png", "paper.png", "scissors.png"]

func chooseWinner(userChoice:Int){
    let randomNumber = Int(arc4random_uniform(3))
    PersonsChoice.image(option[randomNumber])

    if (randomNumber == 0 && userChoice == 1)
        || (randomNumber == 1 && userChoice == 2)
        || (randomNumber == 2 && userChoice == 0) {        
        resultLabel.text("You Win")
    } else if (userChoice == 0 && randomNumber == 1)
        || (userChoice == 1 && randomNumber == 2)
        || (userChoice == 2 && randomNumber == 0) {
        resultLabel.text("I Win!")
    } else {
        resultLabel.text("It's a draw!")
    }       
}

I am also getting the error Cannot call value of non-function type 'String?' for the resultLabel.text. Any help would be much appreciated.

1 Answer 1

2

To set the property image of an existing UIImageView, you need to assign it a new instance of UIImage:

PersonsChoice.image = UIImage(named: option[randomNumber])

Same thing for the text of a UILabel:

resultLabel.text = "It's a draw!"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help Veducm.

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.