首页 > 解决方案 > 当我试图获取 geocodeAddressString 它不断抛出这个错误

问题描述

当我尝试获取 geocodeAddressString 时,当我单击修复按钮时它会不断抛出此错误,它会添加一个“as!CLGeocodeCompletionHandler” 在代码的底部,但它不起作用。编码的屏幕截图

   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.
}


}`

标签: swiftxcodemapkit

解决方案


改变这个

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

对此:

(placemarks, error)

推荐阅读