无法将类型为"ViewController?"的值转换为预期的参数类型"_OptionalNilComparisonType"



我正在我的应用程序中实现MapKit我已经能够显示用户的当前位置。现在我正在尝试将当前街道名称和街道地址显示在我的故事板上的标签上,但我收到此错误:

无法将"ViewController"类型的值转换为此代码行上的预期参数类型"_OptionalNilComparisonType"

guard let self = self else { return }

下面是我的示例代码:

extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let center = getCenterLocation(for: mapView)
let geoCoder = CLGeocoder()
guard let previousLocation = self.previousLocation else { return }
guard center.distance(from: previousLocation) > 50 else { return }
self.previousLocation = center
geoCoder.reverseGeocodeLocation(center) { [weak self] (placemarks, error) in
guard let self = self else { return }
if let _ = error {
//TODO show alert informing the user
return
}
guard let placemark = placemarks?.first else {
//TODO Show alert informing the user
return
}
let streetNumber = placemark.subThoroughfare ?? ""
let streetName = placemark.thoroughfare ?? ""
DispatchQueue.main.async {
self?.addressLabel.text = "(streetNumber) (streetName)"
}
}
}
}

更新到 Xcode 10 和 Swift 4.2。这是 Swift 的最早版本,其中表达式

guard let self = self else { return }

是合法的。

如果无法执行此操作,请在等号的左侧使用其他变量名称。例如:

guard let strongself = self else { return }

相关内容

  • 没有找到相关文章

最新更新