Home IOS Development ios – How can I replace a counter for every `View` a SwiftUI `ForEach` builds?

ios – How can I replace a counter for every `View` a SwiftUI `ForEach` builds?

ios – How can I replace a counter for every `View` a SwiftUI `ForEach` builds?


I believe I titled this query precisely. LOL

This can be a repeat, however I do not perceive methods to apply what I’ve seen to my state of affairs.

I’ve X variety of factors (10 – 1,000+) to plot as iOS 17 Annotations on a MapKit map in SwiftUI. Creating these annotation factors can take a while, so I wish to show a loading standing popup that counts up as every annotation is created (At the moment being accomplished in a SwiftUI ForEachbuilder‘).

At the moment, I’m solely conscious of incrementing an @State variable to do that.

I’m starting to know that ForEach is a builder of Views and never a loop … and it is closure can solely include code that ends in a View/s(?).

I additionally get that currentCount += 1 doesn’t lead to a View.

What I don’t but grasp is the place/methods to increment/monitor one thing that may hold my popup in sync.

I attempted to place all of my ForEach closure code in a perform (as instructed elsewhere), however I couldn’t work out the syntax to make it work.

Here is my code…

//🟣    Show a Map with 10 - 1,000  annotations.
ZStack {
   Map {
         .filter { level in return level.normalizedScoreInt >= Int(severityOfPointsToMap) ? true : false }) {p in
            Annotation("", coordinate: CLLocationCoordinate2D(latitude: p.latitude, longitude: p.longitude), anchor: .topTrailing) {
               if p.normalizedScoreInt >= 5 {
                  ArrowStarView(bumpColor: Colour(p.heatmapColor))
               } else {
                  BallArrowView(bumpColor: Colour(p.heatmapColor), rotation: Double(p.headingDeg))
               currentCount += 1 //  ← ← THIS IS WHAT I WANT (in order that the counter in my popup updates. 
   if debugStatusIsVisible {
      VStack {
         DebugStatusView(debugStatusIsVisible: $debugStatusIsVisible, whatsBeingCounted: $whatsBeingCounted, currentCount: $currentCount, totalToBeCounted: $numberOfPointsToMap)

I’ve googled this and reviewed 10+ outcomes, and so they all appear to be the identical downside from the identical particular person (good job to them for making an attempt to get their reply).

Nevertheless, I do not suppose it’s the identical state of affairs that I’ve.

That is one thing I discover myself desirous to do the entire time. Cannot I write some type of extension to ForEach to permit me to have the ability to replace a counter⁉️ LOL



Please enter your comment!
Please enter your name here