基本的に2つのラベルに2つの乱数を20個まで割り当てようとしています。答えが正しいかどうかに基づいて、別のビューが表示されます。これは10回発生します。 問題は、私が使用するカウンタ "i"にエラーが発生し、変数として宣言しても、定数であるというエラーが表示されることです。値に割り当てることはできません: 'i'は即座に 'let'定数です
@IBAction func submit(sender: AnyObject) {
//declarations
var i: Int //counter for 10 repetitions
var result = 0
for i in 0..<10 {
//generate 2 random numbers up to 20
var rn1 = arc4random_uniform(20)
var rn2 = arc4random_uniform(20)
//assign the rundom numbers to the labels
n1.text = String(rn1)
n2.text = String(rn2)
result = Int((rn1) + (rn2))
//show respective view based on if answer is correct or not
if answer.text == String(result) {
i = i + 1 //here i get the error: cannot assign to value 'i' is a 'let' constant
performSegueWithIdentifier("firstsegue", sender: self)
}else {
performSegueWithIdentifier("wrong", sender: self)
}
}
}
条件が一致するとループを1回壊しますか? –