I have two nested ForEach
s inside a LazyHStack
LazyHStack {
ForEach(items) { item in
ForEach(item.urls, id: \.self) {
Text($0.absoluteString)
}
}
}
This snippets compiles, but it immediately crashes with the following error
Fatal error: each layout item may only occur once: file SwiftUI, line 0
I read online that this might be due to ForEach
not distinguishing correctly the elements of the collection, even if there're all identifiable. In my case, the items
in the outer ForEach
are all identifiable, while the inner ForEach
is looping through an array of optional URL?
objects. I tried to make an URL
identifiable using its absolute string (that should be unique, I think), but it did not work.
extension URL: Identifiable {
var id: String? { absoluteString }
}
I should add that the same code snippet works fine with a standard HStack
. How can I solve this problem?
It is working, you had some mistake in code:
for example you should use
self
in your extensionalso you used 2 ForEach inside together for no reason, I would say bad coding, I am not going advice to use 2 ForEach inside together, it bring down the system and your app, use 1 ForEach! However here is your answer: