2x Execute Statement Inside SwiftUI LazyVStack

Viewed 16

When I execute the code below in an iPhone Simulator w/Xcode 14, I see double the print statements. I had only expected to see one for each rectangle. The executing code only creates one indigo rectangle unit the available space is filled (7 in all on an iPhone 14 Pro). Am wondering why I get a 2x print statement (unexpected) but only one rect (expected)

ScrollView {
    LazyVStack {
        ForEach(0..<1000) { i in
            let _ = print("Dang \(i)")
            Rectangle().fill(.indigo)
                .frame(width: 100, height: 100)
        }
    }
}
.padding()

And here's what the output to the console looks like:

Dang 0
Dang 0
Dang 1
Dang 1
...
Dang 6
Dang 6

Also - curiously, if I change the LazyVStack to just a VStack, then I see one line for each Rectangle created (1000 Dangs, which is expected). Not sure why I see the double with the LazyVStack.

And more curious - if I use a LazyVGrid rather than LazyVStack, I only see one print statement, as expected. Is this a bug or is there something happening with statement execution that I'm not understanding.

0 Answers
Related