简体   繁体   中英

Cannot call value of non-function type 'UIImage?'

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.

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!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM