0

is that a way to get the top parten foreach's index? by two using foreach array?

ForEach(total, id: \.id){ item in
    ForEach(nameber, id: \.id){ item in
        (get the total;s index)
    }
}
koen
  • 5,383
  • 7
  • 50
  • 89
  • 1
    Could you add some sample data and expected output to make it clearer what you want? – Joakim Danielson Sep 18 '21 at 07:11
  • Does this answer your question? [How to iterate a loop with index and element in Swift](https://stackoverflow.com/questions/24028421/how-to-iterate-a-loop-with-index-and-element-in-swift) – Commander Tvis Sep 18 '21 at 12:17

2 Answers2

0

You need to give the content's input different variable names.

You have both of them as item, so if you have different names, you can access the outside variable - not just the inside one.

Example:

struct ContentView: View {
    var body: some View {
        ForEach(0 ..< 5) { i in
            ForEach(0 ..< 5) { j in
                Text("(i: \(i), j: \(j))")
            }
        }
    }
}

Result:

Result

Variables renamed to match your example (using implicit (id: \.id, assuming they conform to Identifiable):

struct ContentView: View {
    var body: some View {
        ForEach(total) { totalItem in
            ForEach(nameber) { nameberItem in
                Text("(totalItem: \(totalItem), nameberItem: \(nameberItem))")
            }
        }
    }
}
George
  • 25,988
  • 10
  • 79
  • 133
0

very criptic question. Try this, to get the index and the item of the outer ForEach:

    ForEach(Array(total.enumerated()), id: \.1) { index, item in
        ForEach(member, id: \.id) { item2 in
            Text(String(index) + " \(item)" + " \(item2)")
        }
    }