spoax
spoax

Reputation: 589

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

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.

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'

Upvotes: 0

Views: 48

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

I think the problem is in 0..<shotCountInt of optional Int , so

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

Upvotes: 1

Related Questions