Stefano Zambrini
Stefano Zambrini

Reputation: 13

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?

Upvotes: 1

Views: 2546

Answers (2)

Tim Newton
Tim Newton

Reputation: 1521

Perhaps this context will help resolve your error (same answer as Gereon)?

Picker("My Value", selection: $myBinding) {
   ForEach(Array(stride(from: 0, to: 100, by: 5)), id: \.self) { index in
      Text("\(index)")
         .tag(index)
      }
}

Upvotes: 2

Gereon
Gereon

Reputation: 17874

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
  ...
}

Upvotes: 7

Related Questions