1

How can I achieve a ForEach result that displays each array member, along with numerical values depending on the list count, that increment up for each item

Example would be 1 "Turkey" 2 "Ham" 3 "Mayo"

struct EditorDirections: View {
    @State private var recipeDirections: [String] = ["Ham", "Turkey", "Mayo"]
   
    
    var body: some View {
        VStack{
            List{
                ForEach(recipeDirections, id: \.self){ recipe in
                    HStack{
                        Text()// show 1, 2, 3
                        Text(recipe)
                    }
                }
           }
Swink
  • 353
  • 3
  • 26

1 Answers1

3

Try using enumerated

ForEach(Array(recipeDirections.enumerated()), id: \.offset){ (index,recipe) in

The index will be 0,1,2, according to array numbers.

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48