Search code examples
swiftfor-loopswiftuiswift5

Range with increments in Picker in SwiftUI


I would like to iterate a range of Int from 0 to 100 with an increment of a value (i.e 5). Is there any func or extension that allows me to create this collection and for looping inside a Picker?


Solution

  • Use stride(from: 0, through: 100, by: 5) as you picker's data source.

    With ForEach, wrap the result in an array like so:

    ForEach(Array(stride(from: 0, to: 100, by: 5)), id: \.self) { index in
      ...
    }