Please take a look at the following simple SwiftUI View:
struct ContentView: View {
var body: some View {
ForEach(1...1, id: \.self) { i in
subview(i)
}
}
func subview(_ i: Int) -> some View
{
print("creating subview \(i)")
return Text("Hello, world!")
}
}
When this View is displayed, all subviews are created twice, as the print statements show. (Unfortunately the Apple Developer Forums UI does not let me attach my sample Xcode project.) This happens on macOS 14.4.1.
Am I doing something wrong or is this a SwiftUI bug? (In a real-world application the View creation can be expensive…)