所以基本上我试图在2个标签中分配2个随机数,最多20个,用户必须找到正确的结果.如果答案是否正确,将出现不同的视图,这将发生10次.问题是我在我使用的计数器"i"上出现错误,即使我将其声明为变量,但我得到一个错误,说它是一个常量.
@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 …Run Code Online (Sandbox Code Playgroud)