0

Do any 1 know any changes on swift3?How to fix the following code I m using geococdeAddressString

class func getMapByAddress(_ locationMap:MKMapView?, address:String?, title: String?, subtitle: String?)
{
            let geocoder = CLGeocoder()
            geocoder.geocodeAddressString(address!, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
                if let validPlacemark = placemarks?[0]{
                    print(validPlacemark.location?.coordinate)

                    let span = MKCoordinateSpanMake(0.05, 0.05)
                    let region = MKCoordinateRegion(center: (validPlacemark.location?.coordinate)!, span: span)
                    locationMap?.setRegion(region, animated: true)

                    let annotation = MKPointAnnotation()
                    annotation.coordinate = (validPlacemark.location?.coordinate)!
                    annotation.title = title
                    annotation.subtitle = subtitle
                    locationMap?.addAnnotation(annotation)
                }

            } as! CLGeocodeCompletionHandler)
}

Error at this line..is just crash at this line as! CLGeocodeCompletionHandler) with no error just showing <private>

Stefan Lam
  • 130
  • 10
  • 1
    Comment your code, then start rewriting it and let Xcode's autosuggest guide you to the new syntax. – Eric Aya Sep 26 '16 at 09:33

1 Answers1

10

Use following code i.e remove [CLPlacemark]? and NSError? from completion handler

class func getMapByAddress(_ locationMap:MKMapView?, address:String?, title: String?, subtitle: String?)
    {
        let geocoder = CLGeocoder()
        geocoder.geocodeAddressString(address!, completionHandler: {(placemarks, error) -> Void in
            if let validPlacemark = placemarks?[0]{
                print(validPlacemark.location?.coordinate)

                let span = MKCoordinateSpanMake(0.05, 0.05)
                let region = MKCoordinateRegion(center: (validPlacemark.location?.coordinate)!, span: span)
                locationMap?.setRegion(region, animated: true)

                let annotation = MKPointAnnotation()
                annotation.coordinate = (validPlacemark.location?.coordinate)!
                annotation.title = title
                annotation.subtitle = subtitle
                locationMap?.addAnnotation(annotation)
            }

            })
    }
Mahendra Y
  • 1,941
  • 20
  • 26