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)
                    }
                }
           }
0

1 Answer 1

4

Try using enumerated

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

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.