无法将类型 'ViewController?' 的值转换为预期的参数类型“_OptionalNilComparisonType”
Cannot convert value of type 'ViewController?' to expected argument type '_OptionalNilComparisonType'
我正在我的应用程序中实施 MapKit
我已经能够显示用户当前位置。现在,我正在尝试将当前街道名称和街道地址显示到故事板上的标签上,但出现此错误::
Cannot convert value of type 'ViewController?' to expected argument type '_OptionalNilComparisonType' on this line of code
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 }
我正在我的应用程序中实施 MapKit
我已经能够显示用户当前位置。现在,我正在尝试将当前街道名称和街道地址显示到故事板上的标签上,但出现此错误::
Cannot convert value of type 'ViewController?' to expected argument type '_OptionalNilComparisonType' on this line of code
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 }