无法在swift中键入“String”类型的值以键入“UILabel”

我正在制作一个关于数学测试​​随机发生器的程序。 但是当我创建一个随机操作时。 我使用arc4random_uniform()来创建一个随机数。 这是function。

func generateQuestion() { var randomoperation:UInt32 = arc4random_uniform(3) if randomoperation == 0 { operation = "+" } if randomoperation == 1 { operation = "-" } if randomoperation == 2 { operation = "X" } if randomoperation == 3 { operation = "/" } } 

这会在swift中创建错误“无法分配值类型”字符串“键入”UILabel“如何解决此问题?

 func generateQuestion() { switch arc4random_uniform(4) { case 0: operation.text = "+" case 1: operation.text = "-" case 2: operation.text = "X" case 3: operation.text = "/" default: operation.text = "" } } 

我认为operation是您的标签名称,因此您可以通过以下方式为其指定文本:

 func generateQuestion() { var randomoperation:UInt32 = arc4random_uniform(3) if randomoperation == 0 { operation.text = "+" } if randomoperation == 1 { operation.text = "-" } if randomoperation == 2 { operation.text = "X" } if randomoperation == 3 { operation.text = "/" } } 

阅读此Apple文档以获取更多信息。