-1

I have mulitple for- in loops but I'm not sure how to bounce back up to the top loop each time for the four geojson records I have that are individual arrays, place, pass and lat/long.

Code:

                for aPlace in place {
                    print(aPlace)
                    for aPass in pass {
                        print(aPass)
                        for var (i, x) in zip(lat, long) {
                            print(i)
                            print(x)

                //These must be in this order to load right at least from what I have seen thus far
                var point = MGLPointAnnotation()
                point.title = aPlace
                point.subtitle = aPass
                point.coordinate = CLLocationCoordinate2D(latitude: i, longitude: x)
                print(point)
                print(" ")
                mapView.addAnnotation(point)
                //after this the code jumps to pass type record2
                    break
                }; break; continue
            }
        }

I have some error checking prints in the there to help me understand what is going on...

this is what I get printed out...

North Shore Trailhead
passRecord1
48.066736
-123.835361
<MGLPointAnnotation: 0x7fa9b3a37630; title = "North Shore Trailhead"; subtitle = "passRecord1"; coordinate = 48.066736, -123.835361>

Lyre RiverTrailhead
passRecord1
48.066736
-123.835361
<MGLPointAnnotation: 0x7fa9b1c2aa40; title = "Lyre RiverTrailhead"; subtitle = "passRecord1"; coordinate = 48.066736, -123.835361>

Having a different trailhead location in record 2 is what I want so that is ok. After that it just records the same passRecord# and same lat/lon which is then reported to the MGLAnnotation which is a mapbox marker.

Is there a better way to do this? I have searched for hours to find an explanation for this to no avail.

Documentation:

AppleDoc has a part on label statements but I can't seem to get that to work either.

if you look at nested loops in this link it will show what the program is doing...just don't know how to fix it...

Staley
  • 55
  • 2
  • 11

1 Answers1

1
  • you are iterating over all places
  • for each place, you are iterating over all passes
  • for each place-pass combination, you are iterating over all i-x combinations
  • the first break is breaking out of the most inner loop, so only the first i-x pair is processed
  • the second break is in the middle loop, so only one pass is processed
  • the continue never gets reached because the second break jumps out here

proposal:

  • comment out the break- and continue statements and observe what happens
  • think about how this is different from what you expect
  • either fix it or come back here and explain to us
  • Ok so when I do that I get this result... North Shore Trailhead passRecord1 48.066736 -123.835361 48.091941 -123.801919 I want trailhead1, passRecord1, lat/lon for point #1, and it printed in annotation form, then trailhead2, passRecord2, lat/lon2, and annotation2 such and so forth through all the records – Staley Jul 10 '16 at 00:12
  • okay, easy version: iterate over all indexes (from 0 to smaller count elements) and then access the proper fields in each list. – user6569971 Jul 10 '16 at 00:31
  • hard version would be to iterate over all arrays at the same time by some fance map-filter-reduce call. you can for example look here if something fits your needs: http://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ – user6569971 Jul 10 '16 at 00:32
  • interesting, think you need to use zip() http://stackoverflow.com/questions/29217690/iterate-over-two-arrays – user6569971 Jul 10 '16 at 00:34
  • maybe you can zip all 4 arrays at once and get out the 4 variables? – user6569971 Jul 10 '16 at 00:37
  • if not, you could merge them with zip(zip(a,b),zip(c,d)) – user6569971 Jul 10 '16 at 00:40
  • these are great suggestions, I think the stackoverflow question is where I learned about zip. I tried zipping all four arrays but not using that method. I'll give it a whirl, if no go, I'll look at map further, seems like a useful tool. I tried the index method but I may have missed something on that in my debugging phase of that portion.. – Staley Jul 10 '16 at 00:46