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)
}
}
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)
}
}
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:
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))")
}
}
}
}
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)")
}
}