简体   繁体   English

不能调用非函数类型'UIImage'的值吗?

[英]Cannot call value of non-function type 'UIImage?'

I am trying to run this code that will randomise a UIImage . 我试图运行这个代码,随机化UIImage But I am getting the following error Cannot call value of non-function type 'UIImage? 但我得到以下错误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?' 我也得到错误Cannot call value of non-function type 'String?' for the resultLabel.text . 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 : 要设置现有UIImageView的属性image ,需要为其分配一个新的UIImage实例:

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

Same thing for the text of a UILabel : UILabel text也是如此:

resultLabel.text = "It's a draw!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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