Iterating over a for loop X amount of times from textfield input

spoax

New Member
#1
I am trying to query my textfield.text input (Let's say the user types 5 in the keypad).
Then convert the string to an integer.
Then make a for loop and run the for loop X amount of times. (X being the integer converted from the string).
I have tried the below however I can't work out the syntax/format for the for loop.
Mã:
var shotCountInt = Int(numberOfShots.text!)

for item in 0..<shotCountInt {
     //do something
}
The error I am getting is on the for loop which is:
Type 'Int?' does not conform to protocol 'Sequence'
 

Admin

Administrator
Thành viên BQT
#2
I think the problem is in 0..<shotCountInt of optional Int , so
Mã:
if let shotCountInt = Int(numberOfShots.text!) {
    for item in 0..<shotCountInt {
       // proceed here
    }
}
 
Top