从文本字段输入迭代for循环X次

时间:2018-09-10 20:53:40

标签: ios swift for-loop

我正在尝试查询我的textfield.text输入(假设用户在键盘上键入5)。

然后将字符串转换为整数。

然后进行for循环,并运行for循环X次。 (X是从字符串转换而来的整数)。

我尝试了以下方法,但是我无法计算for循环的语法/格式。

var shotCountInt = Int(numberOfShots.text!)

for item in 0..<shotCountInt {
     //do something
}

我遇到的错误是在for循环上的:

  

输入'Int?'不符合协议“序列”

1 个答案:

答案 0 :(得分:1)

我认为问题出在可选的 Int 0..<shotCountInt中,所以

if let shotCountInt = Int(numberOfShots.text!) {
    for item in 0..<shotCountInt { 
       // proceed here 
    }
}