0

I know this is probably a simple queston, I would like to return the value of currentLocGeoPoint and return the array of Objects which is of type PFObject.

  1. Tried to save it as a global variable, but it doesn't work because it is asynchronous and doesn't take a value yet. Returns empty.
  2. Tried to return currentLocGeoPoint and changed Void in to PFGeoPoint in. Gives error: PFGeoPoint is not convertible to 'Void'

So I'm not sure how I can fetch the variable currentLocGeoPoint.

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        if (error != nil) {
            println("Error:" + error.localizedDescription)
            //return
        }
        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
            currentLoc = manager.location 
            currentLocGeoPoint = PFGeoPoint(location:currentLoc)
            var query = PFQuery(className:"Bar") 
            query.whereKey("BarLocation", nearGeoPoint:currentLocGeoPoint, withinMiles:10) 
            query.limit = 500
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]!, error: NSError!) -> Void in
                if objects != nil {  
                } else {
                    println("error: \(error)")
                }
            }
        } else {
            println("error: \(error)")
        }
    })
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • The code you posted will not compile. – Kreiri Dec 31 '14 at 13:21
  • @Kreiri i updated with the omitted code – user2913669 Dec 31 '14 at 13:28
  • [Return value for function inside a block](http://stackoverflow.com/questions/17642535/return-value-for-function-inside-a-block) – Kreiri Dec 31 '14 at 13:32
  • @Kreiri thanks for the heads up. is there a swift example, as I couldn't find one anywhere and had trouble implementing into my code :\ – user2913669 Dec 31 '14 at 13:47
  • @Kreiri if the current code i have already has a completionhandler, would I be able to add another as suggested in your linked solution? – user2913669 Dec 31 '14 at 14:15
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 10 '15 at 03:57

2 Answers2

1

I don't understand the notion of "I want to return currentLocGeoPoint". Return it to what? You're in a CLLocationManagerDelegate method, so there's no one to return it to.

What you could do, though, is, when the request is done (i.e. within this closure), call some other function that needed the currentLocGeoPoint. Or you could update the UI to reflect the updated information (make sure to dispatch that update to the main thread, though). Or, if you have other view controllers or model objects that need to know about the new data, you might post a notification, letting them know that there is an updated currentLocGeoPoint. But within this method, there's no one to whom you would "return" the data.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Yes, I have tried calling the other functions that need the `currentLocGeoPoint`. I used the GeoPoint to create an array, but eventually, I would need to pass the value (of the array) back to the main thread and also to pass to the next viewController. – user2913669 Dec 31 '14 at 16:31
  • So, just have the completion closure set whatever properties you want. Just have it call whatever methods you want. If you want second view controller know about it, have the property in that other view controller implement a `didSet` clause in the property. Or observe a notification. Whatever you want. – Rob Dec 31 '14 at 16:44
0

You could assign it to a stored property of your class. Just use

self.<property> = currentLocGeoPoint
qwerty_so
  • 35,448
  • 8
  • 62
  • 86