geocoder.geocodeAddressString no longer works with swift update today
Asked Answered
E

4

5

https://developer.apple.com/library/prerelease/mac/releasenotes/General/APIDiffsMacOSX10_11/Swift/CoreLocation.html

shows that there were a couple of changes

func geocodeAddressString(_ addressString: String!, completionHandler completionHandler: CLGeocodeCompletionHandler!)

to:

func geocodeAddressString(_ addressString: String, completionHandler completionHandler: CLGeocodeCompletionHandler)

my code was:

var geocoder = CLGeocoder()

geocoder.geocodeAddressString("\(event!.street), \(event!.city), \(event!.country)", completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
    if let placemark = placemarks?[0] as? CLPlacemark {
        self.event!.lat = placemark.location!.coordinate.latitude
        self.event!.long = placemark.location!.coordinate.longitude

        self.event!.getMiles(self.currentLocation!.location!.coordinate.latitude, clong: self.currentLocation!.location!.coordinate.longitude)
        var mile = self.event!.miles != nil ? NSString(format: "%.1f miles", self.event!.miles!) : "Location services off"
        self.milesButton.setTitle(mile as String, forState: .Normal)
    }
})

tried:

var geocoder = CLGeocoder()
let address = "\(event!.street), \(event!.city), \(event!.country)"
geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]) in
     let placemark = placemarks[0]
     self.event!.lat = placemark.location!.coordinate.latitude
     self.event!.long = placemark.location!.coordinate.longitude
     self.event!.getMiles(self.currentLocation!.location!.coordinate.latitude, clong: self.currentLocation!.location!.coordinate.longitude)
     var mile = self.event!.miles != nil ? NSString(format: "%.1f miles", self.event!.miles!) : "Location services off"
     self.milesButton.setTitle(mile as String, forState: .Normal)
})

it just keeps saying that its not allowed. tried a few different combinations. this is do to the latest update to xcode / swift

thanks in advance

Equally answered 18/9, 2015 at 15:4 Comment(0)
A
7

Use geocodeAddressString this way:

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

And it will work fine.

Accelerate answered 18/9, 2015 at 15:34 Comment(1)
Happy to help you..:)Accelerate
C
10

In Swift 2:

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

In Swift 3, replace NSError? with Error?:

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: Error?) -> Void in

})

Or, easier, just let it infer the correct types for you:

geocoder.geocodeAddressString(address) { placemarks, error in

}
Contrary answered 17/10, 2016 at 23:6 Comment(0)
A
7

Use geocodeAddressString this way:

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

And it will work fine.

Accelerate answered 18/9, 2015 at 15:34 Comment(1)
Happy to help you..:)Accelerate
C
2
geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

})

Just change NSError to Error and will it work

Carrico answered 27/9, 2016 at 11:40 Comment(0)
D
2

This no longer works....

  geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
        print("Test")
    } as! CLGeocodeCompletionHandler)

throws an EXC error

Dulosis answered 17/10, 2016 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.