我正在尝试查询我的textfield.text输入(假设用户在键盘上键入5)。
然后将字符串转换为整数。
然后进行for
循环,并运行for
循环X次。 (X是从字符串转换而来的整数)。
我尝试了以下方法,但是我无法计算for循环的语法/格式。
var shotCountInt = Int(numberOfShots.text!)
for item in 0..<shotCountInt {
//do something
}
我遇到的错误是在for循环上的:
输入'Int?'不符合协议“序列”
答案 0 :(得分:1)
我认为问题出在可选的 Int 的0..<shotCountInt
中,所以
if let shotCountInt = Int(numberOfShots.text!) {
for item in 0..<shotCountInt {
// proceed here
}
}