Two lists in a view getting their text confused

I have a Form with two List sub-views, each in its own section. I populate these with the basic ForEach range pattern:

Code Block
Form {
Section {
List {
ForEach(0..<array1.count, id: \.self) { idx in
NavigationLink(another_view_1()) {
Text(array1[idx].name)
}
}
}
}
Section {
List {
ForEach(0..<array2.count, id: \.self) { idx in
NavigationLink(another_view_2()) {
Text(array2[idx].name)
}
}
}
}
}


The first list works fine. When I scroll down to the second list, some of the names (and navigation links and their destination views) are from the first list! The name properties for some of the elements of the second list are invoked, and they show up properly. Some elements don't have the name property invoked, however, and they show up (and work as if) they were from the first list.

This seems like an outright bug in SwiftUI -- anyone seen it before? Are there any work arounds?

I just run this :

Code Block
struct Item {
    var name = ""
}
var array1 = [Item(name: "1a"), Item(name: "1b")]
var array2 = [Item(name: "2a"), Item(name: "2b")]
struct MyForm: View {
    var body: some View {
        NavigationView {
            Form {
                Section {
                    List {
                        ForEach(0..<array1.count, id: \.self) { idx in
                            NavigationLink(destination: Text("1")) {
                                Text(array1[idx].name)
                            }
                        }
                    }
                }
                Section {
                    List {
                        ForEach(0..<array2.count, id: \.self) { idx in
                            NavigationLink(destination: Text("2")) {
                                Text(array2[idx].name)
                            }
                        }
                    }
                }
            }
        }
    }
}


All went great.

This seems like an outright bug in SwiftUI -- anyone seen it before?

This seems to be a bug of your code. Or you can call it as a specification bug.
When you specify id in ForEach, the value of id needs to be able to identify a single element in all the lists.

Define an extension for Int:
Code Block
extension Int {
var secondaryKey: Int {
1 << 16 + self
}
}

And modify your second ForEach like this:
Code Block
Form {
Section {
List {
ForEach(0..<array1.count, id: \.self) { idx in
NavigationLink(destination: AnotherView1()) {
Text(array1[idx].name)
}
}
}
}
Section {
List {
ForEach(0..<array2.count, id: \.secondaryKey) { idx in //<-
NavigationLink(destination: AnotherView2()) {
Text(array2[idx].name)
}
}
}
}
}


Please try.
Yes, I had settled on a workaround much like your suggestion and the problem goes away. This might be a problem of specification, but it definitely violates the principle of least astonishment!! A highly questionable design decision, IMO.
Two lists in a view getting their text confused
 
 
Q