簡體   English   中英

不能快速分配“ String”類型的值來輸入“ UILabel”

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

我正在編寫一個有關數學測試隨機生成器的程序。 但是,當我創建一個隨機操作時。 我使用arc4random_uniform()創建一個隨機數。 這是功能。

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

這會產生錯誤“無法迅速分配值類型“ String”來鍵入“ 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文檔以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM