0
   func getLatsAndLongs() -> (LATITUDE: Double, LONGITUDE: Double, DESIREDLAT: Double, DESIREDLONG: Double) {



    self.forwardGeocoding("\(addressTxtFld.text) \(cityTxtFld.text), \(stateTxtFld.text)", completion: {
        success, coordinate in

        if success {
            self.lat = coordinate.latitude
            self.long = coordinate.longitude

            print("\(self.lat) is the latitude for the initial location")
            print("\(self.long) is the longitude for the initial location")

            self.INITIAL_DESTINATION_LATITUDE = self.lat
            self.INITIAL_DESTINATION_LONGITUDE = self.long

            var initialLocation = CLLocationCoordinate2DMake(self.INITIAL_DESTINATION_LATITUDE, self.INITIAL_DESTINATION_LONGITUDE)



        } else {
            print("Error at forwardGeocoding @willcohen @ERRORALERT")
        }
    })


    self.forwardGeocodingDesired("\(addressTxtFldDest.text) \(cityTxtFldDest.text), \(stateTxtFldDest.text)", completion: {
        success, coordinate in

        if success {
            self.desiredLat = coordinate.latitude
            self.desiredLong = coordinate.longitude

            print("\(self.desiredLat) is the latitude for the desired location")
            print("\(self.desiredLong) is the longitude for the desired locaiton")

            self.DESIRED_DESTIANTION_LATITUDE = self.desiredLat
            self.DESIRED_DESTINATION_LONGITUDE = self.desiredLong

            var desiredLocation = CLLocationCoordinate2DMake(self.DESIRED_DESTIANTION_LATITUDE, self.DESIRED_DESTINATION_LONGITUDE)




        } else {
            print("Error at forwardGeocodingDesired @willcohen @ERRORALERT")
        }

    })

    return (lat,long,desiredLat,desiredLong)

}


           let latsAndLongs = getLatsAndLongs()

        let latFF = latsAndLongs.LATITUDE
        let longFF = latsAndLongs.LONGITUDE
        let latDFF = latsAndLongs.DESIREDLAT
        let longDFF = latsAndLongs.DESIREDLONG

        print("\(latFF) final lat")
        print("\(longFF) final long")
        print("\(latDFF) final latD")
        print("\(longDFF) final longD")

Okay. So, when I try to print all of the lines on the last 4 lines, it comes out as "0" each time. Note, the two geocoding lines (self.forwardGeocoding) & (self.forwardGeocodingDesired) are not the problems, they work fine, but I have no idea why they are not printing the correct Double values. Any suggestions would be greatly appreciated, thank you.

2 Answers 2

1

forwardGeocoding and forwardGeocodingDesired invoke an operations over the network which takes time to executes. This is done using a background thread so as not to block the UI. When the operations complete the code in the completion closures is executed.

Your getLatsAndLongs function returns lat,long,desiredLat and desiredLong before the operations have completed and therefore before these variables have been set by your closure.

You can pass a completion handler to getLatsAndLongs to be invoked after the operations are complete, but your situation is complicated because you are executing two geocoding operations in parallel, and you don't know which will finish first. You could dispatch the second one from the completion handler of the first, but this will be slower. A better approach is to use a dispatch_group:

func getLatsAndLongs(completion:(initialLocation: CLLocationCoordinate2D?, desiredLocation: CLLocationCoordinate2D?)->Void)  {

    let dispatchGroup = dispatch_group_create()

    var initialLocation: CLLocationCoordinate2D?
    var desiredLocation: CLLocationCoordinate2D?

    dispatch_group_enter(dispatchGroup)

    self.forwardGeocoding("\(addressTxtFld.text) \(cityTxtFld.text), \(stateTxtFld.text)", completion: {
        success, coordinate in
        if success {
            initialLocation = coordinate
        } else {
            print("Error at forwardGeocoding @willcohen @ERRORALERT")
        }
        dispatch_group_leave(dispatchGroup)
    })


    self.forwardGeocodingDesired("\(addressTxtFldDest.text) \(cityTxtFldDest.text), \(stateTxtFldDest.text)", completion: {
        success, coordinate in

        if success {
            desiredLocation = coordinate
        } else {
            print("Error at forwardGeocodingDesired @willcohen @ERRORALERT")
        }
        dispatch_group_leave(dispatchGroup)
    })

    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue()) {
        completion(initialLocation:initialLocation,desiredLocation:desiredLocation)
    }
}

Usage:

getLatsAndLongs { (initialLocation, desiredLocation) in
    print(initialLocation)
    print(desiredLocation)
}
Sign up to request clarification or add additional context in comments.

3 Comments

I made the edits that you made, and then called it like you said, however I got an error in my appDelegate.swift which is Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Possibly something was nil and you force unwrapped it. Also, since I didn't have the source for forwardGeocodingDesired and forwardGeocoding I made an assumption that coordinate is a CLLocationCoordinate2D - This may be incorrrect and you should adjust things accordingly
Which line did the crash occur on? Try setting an exception breakpoint
0

Check if the completion blocks in self.forwardGeocoding and self.forwardGeocodingDesired execute asynchronously. If so, the values will not be set until after the prints have run.

1 Comment

they do execute asynchronously. If you don't mind, how would I fix this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.