简体   繁体   中英

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

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?

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

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.

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