3

I currently have two fields/properties within my view controller. We are using the calculateDirectionsWithCompletionHandler and trying to set my fields to the value of route.distance and route.expectedTravelTime. Here is the code for that:

func calculateDistanceAndEta(locationCoordinate: CLLocationCoordinate2D) {
        let currentLocMapItem = MKMapItem.mapItemForCurrentLocation();
        let selectedPlacemark = MKPlacemark(coordinate: locationCoordinate, addressDictionary: nil);
        let selectedMapItem = MKMapItem(placemark: selectedPlacemark);
        let mapItems = [currentLocMapItem, selectedMapItem];
        let request: MKDirectionsRequest = MKDirectionsRequest()
        request.transportType = MKDirectionsTransportType.Walking;
        request.setSource(currentLocMapItem)
        request.setDestination(selectedMapItem);
        var directions: MKDirections = MKDirections(request: request);
        var distsanceLabelTest = ""
        var etaLabelTest = ""
        directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
            if (error == nil) {
                if (response.routes.count > 0) {
                    var route: MKRoute = response.routes[0] as! MKRoute;
                    //                    route.distance = distance
                    //                    route.expectedTravelTime = eta
                    println("\(route.distance)")
                    distsanceLabelTest = "\(route.distance)"
                    etaLabelTest = "\(route.expectedTravelTime)"
                }
            } else {
                println(error)
            }
        }
        println(distsanceLabelTest)
        println(etaLabelTest)
        self.distanceLabelString = distsanceLabelTest
        self.etaLabelString = etaLabelTest

    }   

However, we can't seem to set any of the variables as it just returns nil. How do we set our class fields to the values of route.distance and route.expectedTravelTime.

Varun Varahabotla
  • 548
  • 2
  • 7
  • 16

1 Answers1

1

we can't seem to set any of the variables as it just returns nil.

The point of providing a completion block is that calculateDirectionsWithCompletionHandler runs asynchronously and executes the completion routine when it's ready. So your distance and expectedTravelTime properties will indeed be unchanged immediately after calculateDistanceAndEta returns because the process started by calculateDirectionsWithCompletionHandler may not have finished by then. Your completion block will be run when it does finish. If you need to take some action when the properties are set, put that code in your completion block.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • However, the `println(route.distance)` does work and it does print the value correctly, but it just doesn't set the values. So how do we get around this? IN the grand scheme of things, I want to set a value within a custom tableview cell, which is set in the `cellForRowAtIndexPath` delegate method – Varun Varahabotla Sep 06 '15 at 04:13
  • Uncommenting the lines that set the properties would be a step in the right direction. – Caleb Sep 06 '15 at 05:13
  • Is there one canonical post that answers the "why can't I get values out of a block" question? If not, there should be... – jtbandes Sep 06 '15 at 05:38