Search code examples
swiftswiftuipicker

Set picker starting with two digit numbers (from 00) - SwiftUI


I have a ForEach inside a picker with numbers from 0 to 20 that shows 0, 1, 2...20 but instead I need to display 00, 01, 02...20. How can I do it?


Solution

  • You use the normal string formatting String(format: "%02d", 5):

    @State var num: Int = 0
    
    var body: some View {
       Picker("", selection: $num) {
          ForEach(0..<21) { v in 
             Text(String(format: "%02d", v)) 
          }
       }
    }