|
Edited: First time posting. In my application I am creating various arrays of Identifiable structs using my API response. I am iterating over said array to build lists within the Content and Sidebar columns of my Navigation Split View. If I print the array before my ForEach call, the array is normal. When printing each item from within the ForEach (let _ = print(item)) the item prints twice. However the item is only added to the List once. Is this normal behavior? It appears to be happening with all of my ForEach calls. Visually the view looks correct, just want to be sure there isn’t any additional looping or view updated occurring. Thanks in advanced Printing each item of array. Resulting in duplicate prints.
|
|
I've never seen
Unfortunately, I don't know the reason it is happening, and my searches online haven't come up with anything on this yet. But I don't think it is anything that you have done wrong with your code. It just seems to have something to do with the way I don't think that they ever intended for you to be able to include non-View conforming code in the ForEach content, and this way of printing within a closure that is supposed to conform to View seems kind of like a bit of a hack that somebody figured out. |
|
@FlyOstrich takes a guess:
I think coming from a background with "traditional" languages like C++, Java, Python, etc we tend to think of Instead, in a view struct, I like to think of ForEach as a view factory. See -> View Factory Take a look at @Fly's simple example. The goal is to create a Is there enough space? Is the screen wide enough? Does the view have to make room for a There are many calculations to consider. Consequently, SwiftUI may create and destroy subviews several times before settling in on a final presentation view. This is the power of So it's not surprising that you'll see your print statment more than once in the console. What may be more surprising is that you only see the print statement twice! It might be fun to experiment with the This is partly a guess, and partly gleaned from other tutorials. Honestly, I'm not sure either. |
|
@Obelix tries an experiment: I experimented with @Fly's code. Added random height.
The |
|
I am fascinated by this problem. I ran @Fly0strich's and @Obelix's code and they both print just once for me. I saw your question on Stack and the answer was it's normal behavior. That made me even more curious, why
mine prints once. It won't answer my question, but i was wondering if it might show the body rendering
twice using |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.