0

数学テストの乱数発生器に関するプログラムを作成しています。しかし、ランダム操作を作成している間。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 = "/"
        }
    }

これにより、「Swift で型 "UILabel" に値 "String" の型を割り当てることができません」というエラーが作成されます。これをどのように修正しますか?

4

2 に答える 2

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 = ""
    }
}
于 2015-07-01T05:46:22.127 に答える
0

ラベル名だと思う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 ドキュメントを参照してください。

于 2015-07-01T05:35:37.310 に答える