Cannot assign a value of type “String” to type “UILabel” in swift

南楼画角 提交于 2019-12-31 06:54:07

问题


I'm making a program that 's about a math test random generator. But while I was creating a random operation. I used arc4random_uniform() to create a random number. Here's the 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 = "/"
        }
    }

This creates the error "Cannot assign a type of value "String" to type "UILabel" in swift" How do you fix this?


回答1:


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 = ""
    }
}



回答2:


I think operation is your label name so you can assign text to it this way:

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 = "/"
    }
}

Read this Apple Documentation for more Information.



来源:https://stackoverflow.com/questions/31153207/cannot-assign-a-value-of-type-string-to-type-uilabel-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!