Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Lewis Edmonds
Lewis Edmonds
2,823 Points

Extract array from closure in for-in loop before passing through segue

Hi,

I have a closure calculating distances within a for-in loop. This loop produces arrays called distances and journeyTimes, which I can see printing from within the closure.

However, I would like to pass these arrays through to another View Controller via a segue, so I need to be able to access them from outside of the closure.

I know the order is asynchronous, so the segue is passing through an empty array before the loop has had a chance to be populated, but I don't know what the best approach to tackle this problem is.

(I am using a prepare for segue further on in my code to try to pass my arrays, as well as others through)

I have tried dispatchGroup() enter/leave and dispatchGroup.notify to perform a segue, but I can't seem to get away from the closure or passing through an empty array.

Any help is greatly appreciated. Code below:

    @IBAction func calculate(_ sender: Any) {

        for index in 0...coordsOfCollective.count { 

            let sourceLocation = MKPlacemark(coordinate: coordsOfCollective[index])

            let destinationLocation = MKPlacemark(coordinate: coordsOfCollective[index + 1])

            let sourceMapItem = MKMapItem(placemark: sourceLocation)
            let destinationMapItem = MKMapItem(placemark: destinationLocation)

            let request = MKDirectionsRequest()
            request.source = sourceMapItem
            request.destination = destinationMapItem

            request.transportType = .automobile
            request.requestsAlternateRoutes = false

            let directions = MKDirections(request: request)

            directions.calculate { response, error in
                if let route = response?.routes.first {
                   print("Distance: \(route.distance/1000) km, ETA: \(route.expectedTravelTime/60) mins")

                self.distances.append(route.distance/1000)
                self.journeyTimes.append(route.expectedTravelTime/60)

                    print(self.distances)
                    print(self.journeyTimes)


                } else {
                    print("Error!")
                }

            }
        }
    }

Thanks

Lewis

Jhoan Arango
Jhoan Arango
14,575 Points

Hello,

You can use a completion handler, when the loop finishes populating the array, it will pass the information.

That's an idea.