swift - 当我试图获取 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.
}
}`
解决方案
改变这个
(placemarks: [CLPlacemark]?, error: NSError?) -> Void
对此:
(placemarks, error)
推荐阅读
- javascript - p5.js Flood Fill(桶工具)工作缓慢而奇怪
- regex - 城市和街道名称的正则表达式
- scala - 我可以使用 Scala 访问 Companion Class 中的 Companion Object 属性吗
- python - 无法从图像中提取文本
- ssl - 升级后的 Jetty SSL 问题 - 未知协议
- python - 从 python 中的 cvs 中删除 99999 个值?
- user-interface - Fyne 导入问题
- python - 有没有办法在 sci-kit 学习交叉验证中定义我想要的每个标签的分数?
- java - 在 Java 中使用的圣诞树
- python - 删除字段 ogr 图层后尝试迭代功能时出现段错误