I made a property in my view controller:
var cityPlaceMark = CLPlacemark()
And I want to assign it to a placemark in my locationManger didUpdate location function:
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler:{(placemarks, error)->Void in
if (error != nil) {
println("Reverse geocode failed with error")
return
}
if placemarks.count > 0 {
//var pm = placemarks[0] as CLPlacemark
self.cityPlaceMark = placemarks[0] as CLPlacemark
}
})
The app crashes with a Thread1: EXC_BAD_ACCESS.
If I save it in a variable declared in the closure (shown as "var pm" commented out) it works fine.
Can someone explain to me what I am doing wrong, and how to properly assign it to a property.
Thanks!
It looks like it may be a bug.
If I make it into an optional with:
var cityPlaceMark: CLPlacemark?
and implement it in the closure:
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
if (error != nil) {
println("Reverse geocode failed with error")
return
}
if placemarks.count > 0 {
self.cityPlaceMark = placemarks[0] as? CLPlacemark
}
})
It now works!
Related
I am creating a location based app where I want the user to be see details of their location. For showing their address details, I have added a new function, CLGeocoder, for finding the details. However, I get this error:
Initializer for conditional binding must have optional type, not
'CLPlacemark'.
I get the error in:
if let p = CLPlacemark(placemark: placemarks![0] as! CLPlacemark )
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
if (error != nil) {
print(error)
}
else {
if let p = CLPlacemark(placemark: placemarks![0] as! CLPlacemark ) {
}
}
}
You don't have to create the CLPlacemark instance explicitly, reverseGeocodeLocation returns [CLPlacemark]? on success.
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) in
if error != nil { // no parentheses in Swift!
print(error!)
}
else if let p = placemarks?.first {
// p is a non-optional CLPlacemark instance
// Do stuff
}
}
The expression inside the if-let needs to be optional. CLPlacemark initializer will never return an optional value. If you're unsure whether placemarks is nil or not, then something like
if let placemarkArray = placemarks {
let p = CLPlacemark(placemark: placemarkArray[0])
// Do stuff
}
}
Otherwise, you don't need the if let...just let.
I'm pretty much new to GCD. I have this function for forward geocoding and the issue is it returns before the completion closure completes. So every time it just returns nil. I found out I can use semaphores so the return waits for the completion closure to complete, but there are very little examples online and I found none of a function that returns. I tried to implement it, but function still returns nil even though the location is printed out to the console moments later. If someone could tell me where I am making a mistake I would be very grateful.
func forwardGeocoding(address: String) -> CLLocation? {
var userLocation: CLLocation?
var returnvalue: CLLocation?
let semaphore = dispatch_semaphore_create(0)
CLGeocoder().geocodeAddressString(address, completionHandler: { (placemarks, error) in
if error != nil {
print("Geocoding error: \(error)")
return
}
if placemarks?.count > 0 {
let placemark = placemarks?.first
let location = placemark?.location
let coordinate = location?.coordinate
print("Settings location: \(coordinate!.latitude), \(coordinate!.longitude)")
if let unwrappedCoordinate = coordinate {
let CLReadyLocation: CLLocation = CLLocation(latitude: unwrappedCoordinate.latitude, longitude: unwrappedCoordinate.longitude)
userLocation = CLReadyLocation
dispatch_semaphore_signal(semaphore)
}
}
})
let wait = dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
if wait != 0 {
returnvalue = userLocation
}
return returnvalue
}
As Paulw11 already mentioned, a semaphore in this case is very bad programming habit. If you are new to GCD learn to understand the asynchronous pattern for returning received data in a completion block. It's even simpler to handle in Swift than in Objective-C.
This is an example using a completion block:
func forwardGeocoding(address: String, completion: (CLLocation?, NSError?) -> Void) {
CLGeocoder().geocodeAddressString(address, completionHandler: { (placemarks, error) in
if error != nil {
completion(nil, error!)
} else {
if let placemarks = placemarks where !placemarks.isEmpty {
let placemark = placemarks.first!
if let unwrappedLocation = placemark.location {
let coordinate = unwrappedLocation.coordinate
print("Settings location: \(coordinate.latitude), \(coordinate.longitude)")
let CLReadyLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
completion(CLReadyLocation, nil)
}
}
}
})
}
and call it with
forwardGeocoding("foo") { (location, error) in
if error != nil {
print("Geocoding error: \(error!)")
} else {
// do something with the location
}
}
the result of dispatch_semaphore_wait is 0 on success and non zero on timeout occurred. So you should change your code to:
let wait = dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
if wait == 0 { //your work is done, without time out.
returnvalue = userLocation //update location
}
return returnvalue //otherwise return nil according to your code above. this code will never execute. In this case, there is no time out cause it wait forever.
Another point, you must call dispatch_semaphore_signal before your block geocodeAddressString end the execution. Otherwise, your application will wait forever in case of getting error.
CLGeocoder().geocodeAddressString(address, completionHandler: { (placemarks, error) in
if error != nil {
print("Geocoding error: \(error)")
dispatch_semaphore_signal(semaphore) //add to here
return
}
if placemarks?.count > 0 {
let placemark = placemarks?.first
let location = placemark?.location
let coordinate = location?.coordinate
print("Settings location: \(coordinate!.latitude), \(coordinate!.longitude)")
if let unwrappedCoordinate = coordinate {
let CLReadyLocation: CLLocation = CLLocation(latitude: unwrappedCoordinate.latitude, longitude: unwrappedCoordinate.longitude)
userLocation = CLReadyLocation
}
}
dispatch_semaphore_signal(semaphore) //and here
})
Finally, when using semaphore that wait forever, you have to make sure that the block of code inside semaphore will end the execution.
I have an external variable chooseCoordinates, which has a type of CLLocationCoordinate2D. This var need to save coordinates from geocodeAddressString closure, but, apparently, it doesn't change.
I would like to ask for advice, how to make this actually closure store the data, so I will be able to parse it to another viewController
var chooseCoordinates = CLLocationCoordinate2D()
////////////
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(sender.text!, completionHandler: { (placemarks, error) -> Void in
if(error != nil) {
print("\(error)")
}
if let placemark = placemarks?.first {
let coordinates: CLLocationCoordinate2D = placemark.location!.coordinate
self.chooseCoordinates = coordinates
}
})
The geocodeAddressString runs asynchronously (i.e. even though the method returns immediately, its completionHandler closure may be called later). So, are you sure it's not changing, or just not changing by the time you try to use chooseCoordinates? You should initiate whatever updating of the UI or whatever from within the closure (or the didSet of chooseCoordinates), not doing so immediately. We don't see how you're using chooseCoordinates, so it's hard to be more specific than that.
For example:
geocoder.geocodeAddressString(sender.text!) { placemarks, error in
if error != nil {
print(error!)
}
if let placemark = placemarks?.first {
let coordinates: CLLocationCoordinate2D = placemark.location!.coordinate
self.chooseCoordinates = coordinates
// call the method that uses `chooseCoordinates` here
}
}
// don't try to use `chooseCoordinates` here, as it hasn't been set yet.
Or, you might employ the completionHandler pattern yourself:
#IBAction func didEndEditingTextField(sender: UITextField) {
geocodeAddressString(sender.text!) { coordinate, error in
self.chooseCoordinates = coordinate
// trigger whatever the next step is here, or in the `didSet` of `chooseCoordinates`
}
// don't try to use `chooseCooordinates` here
}
var chooseCoordinates: CLLocationCoordinate2D? // you probably should make this optional
let geocoder = CLGeocoder()
/// Geocode string
///
/// - parameter string: The string to geocode.
/// - parameter completionHandler: The closure that is called asynchronously (i.e. later) when the geocoding is done.
func geocodeAddressString(string: String, completionHandler: (CLLocationCoordinate2D?, NSError?) -> ()) {
geocoder.geocodeAddressString(string) { placemarks, error in
if error != nil {
print(error!)
}
if let placemark = placemarks?.first {
completionHandler(placemark.location!.coordinate, error)
} else {
completionHandler(nil, error)
}
}
}
I reverse geocode in a few places in my map and I wanted to wrap it to some common method. What I have is this as a starting point that works:
func reverseGeocodeLocation(location: CLLocation, completion: (CLPlacemark) -> Void) {
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) {
(placemarks, error) -> Void in
// Check for returned placemarks
if let placemarks = placemarks where placemarks.count > 0 {
let topResult = placemarks[0] as! CLPlacemark
completion(topResult)
}
}
}
However, if I want to add another parameter to my completion method that would return the error to my view like so:
func reverseGeocodeLocation(location: CLLocation, completion: (CLPlacemark, NSError) -> Void) {
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) {
(placemarks, error) -> Void in
// Check for returned placemarks
if let placemarks = placemarks where placemarks.count > 0 {
let topResult = placemarks[0] as! CLPlacemark
completion(topResult, error)
}
}
}
If error is nil, then my completion(topResult, error) will fail because error is nil with bad access. But I cannot do this since the parameters are not matching the completion handler.
if error != nil {
completion(topResult)
} else {
completion(...) // do something with the error
}
So if I have a nil error, how would I call my completion handler?
Since you own definition of your completion header your can define it normally with optional NSError? So it will not cause errors and will require unwrapping. Moreover recent definition of CLGeocodeCompletionHandler also uses optional array and error:
typealias CLGeocodeCompletionHandler = ([CLPlacemark]?, NSError?) -> Void
I'm trying to parse a location (CLLocation) into a String.
func locationToString (currentLocation: CLLocation) -> String? {
var whatToReturn: String?
CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
if error == nil && placemarks.count > 0 {
let location = placemarks[0] as CLPlacemark
whatToReturn = "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)"
}
})
return whatToReturn
}
Obviously, whatToReturn always returns null, because completionHandler runs in the background.
I'm having a hard time understanding how do I update my String when completionHandler finishes?
Thanks.
If you want to use your string in a textField, like indicated in your comments, do this:
func getAndDisplayLocationStringForLocation(currentLocation: CLLocation) {
CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
if error == nil && placemarks.count > 0 {
let location = placemarks[0] as CLPlacemark
self.textField.text = "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)"
}
})
}
However, if you need access elsewhere, perhaps pass a closure as an arg:
func getAndDisplayLocationStringForLocation(currentLocation: CLLocation, withCompletion completion: (string: String?, error?, error: NSError?) -> ()) {
CLGeocoder().reverseGeocodeLocation(currentLocation, completionHandler: { (placemarks: [AnyObject]!, error: NSError!) in
if error == nil && placemarks.count > 0 {
let location = placemarks[0] as CLPlacemark
completion(string: "\(location.locality) \(location.thoroughfare) \(location.subThoroughfare)", error: nil)
} else {
completion(nil, error)
}
})
}
Then call like this:
yourModel.getAndDisplayLocationStringForLocation(someLocation) { (string: String?, error: NSError?) -> () in
if (error == nil) {
self.textField.text = string
}
}
You may want to handle error etc. differently. This should be enough to get you started.