-1

I am having a lot of difficulty wrapping my head around completion handlers. And I have no idea how to use this.

Here is the function with the completion handler to obtain geocoordinates based on a string address:

func getLocation(from address: String, completion: @escaping (_ location: CLLocation?)-> Void) {
        guard let address = address as? String else { return }
        let geocoder = CLGeocoder()
        geocoder.geocodeAddressString(address) { (placemarks, error) in
            guard let placemarks = placemarks,
            let location = placemarks.first?.location else {
                completion(nil)
                return
            }
            completion(location)
        }
    }

And I am calling this function like this:

getLocation(from: address) { location in
            print("Location is", location.debugDescription)
            if case self.currentLocation = location {
                self.queryGenerator(searched: self.searchController.isActive, queryString: "")
            } else {
                self.showAlert(alertTitle: "No businsesses nearby", message: "Try going back and changing the address")
            }
        }

I keep getting a nil for currentLocation. If somebody could dumb this down for me, that would be great.

I make a network call after obtaining the location which sends me a custom object that contains the coordinates of the business and a service radius. If the service radius is less than or equal to the distance between the the business coordinates and the currentLocation then I add it to the an array which populates a UITableView. Here is the function where I do the aforementioend calculation:

func applicableRestaurants(allQueriedRestaurants: [Restaurant]) -> [Restaurant] {
        var filteredRestaurants = [Restaurant]()
        for thisRestaurant in allQueriedRestaurants {
            let restaurantCoordinate = CLLocation(latitude: thisRestaurant.geoPoint.latitude, longitude: thisRestaurant.geoPoint.longitude)
            if restaurantCoordinate.distance(from: self.currentLocation!) <= thisRestaurant.distance {
                filteredRestaurants.append(thisRestaurant)
            }
        }
        return filteredRestaurants
    }
BVB09
  • 805
  • 9
  • 19
  • `guard let address = address as? String else { return }` you might want to do `completion(nil)` in case the address isn't good, but that's strange since `address` shouldn't be optional. Is `placemarks` nil? Which case is triggered in `geocodeAddressString()`? – Larme Apr 16 '20 at 16:34
  • Sorry, I dont quite follow. I do have completion(nil) if the location does not resolve. – BVB09 Apr 16 '20 at 16:36
  • Got it. I added the function and an explanation in the question above. – BVB09 Apr 16 '20 at 16:56

1 Answers1

0

My guess (and it is only a guess) is that you have used this expression without understanding what it means:

if case self.currentLocation = location {
    self.queryGenerator...

I think what you probably meant is:

if let location = location {
    self.currentLocation = location
    self.queryGenerator...

I think you may have thought those are equivalent, but they aren't.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ok, I know what you mean now. A bit of a tubelight moment for me. I just started learning Swift 3 months ago. Thanks, that helped a lot. – BVB09 Apr 16 '20 at 17:23
  • Yeah, the `case` pattern syntax is really horrible. – matt Apr 16 '20 at 17:39