当我尝试获取geocodeAddressString时,它会不断抛出此错误

时间:2018-11-14 04:34:34

标签: swift xcode mapkit

当我尝试获取geocodeAddressString时,当我单击“修复”按钮时,它始终抛出此错误,并添加了“ as!”。 CLGeocodeCompletionHandler”,位于代码底部,但不起作用。 Screen Shot of Coding

   import UIKit
   import MapKit

class ViewController: UIViewController {

@IBOutlet weak var addressButton: UITextField!
@IBAction func showMeWhere(_ sender: Any)
{
    //Defining destination
    guard let latitude = CLLocationDegrees(addressButton.text!) else {
            // show some sort message to the user that the values are invalid
            return

            let location = self.addressButton.text;
            let geocoder:CLGeocoder = CLGeocoder();
            geocoder.geocodeAddressString(location!) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
                if placemarks?.count > 0 {
                    let topResult:CLPlacemark = placemarks![0];
                    let placemark: MKPlacemark = MKPlacemark(placemark: topResult);

                    var region: MKCoordinateRegion = self.mkMapView.region;
                    region.center = (placemark.location?.coordinate)!;
                    region.span.longitudeDelta /= 8.0;
                    region.span.latitudeDelta /= 8.0;
                    self.mkMapView.setRegion(region, animated: true);
                    self.mkMapView.addAnnotation(placemark);

                }
            }
    }

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}`

1 个答案:

答案 0 :(得分:0)

只需更改

(placemarks: [CLPlacemark]?, error: NSError?) -> Void 

对此:

(placemarks, error)