问题描述
我正在尝试在Swift 2中的地图上绘制一条折线.这一切都很好,但是对于此代码,我得到了编译器警告:
I am trying to draw a polyline on a map in Swift 2. It all works well, but I get a compiler warning for this code:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.redColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
这会给我一个警告,指出" mapView(rendererForOverlay)中的结果和参数的可选性与协议MKMapViewDelegate预期的不同"
This will give me a warning says that 'Result and parameters in mapView (rendererForOverlay) have different optionality than expected by protocol MKMapViewDelegate'
现在,这可以正常编译,但是让我感到困惑的是正在显示编译器警告.
Now, this will compile fine, but it bugs me that the compiler warning is showing.
如果我将第一行更改为
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
通过删除!,警告将消失,但我得到一个错误,即返回值不能为nil,并且代码将不再编译.
by removing the !, the warning will go away but I get an error that the return cannot be nil and the code will not compile anymore.
这也是该线程的后续操作,在该线程中曾提出相同的问题,但没有令人满意的答案:迅速2 MKMapViewDelegate rendererForOverlay可选性
This is also a follow up to this thread where the same problem was stated but no satisfying answer is available:Swift 2 MKMapViewDelegate rendererForOverlay optionality
现在有人可以在Swift 2中阐明使用此功能的正确方法吗?
Can anyone shed any light on the correct way to use this function now in Swift 2?
谢谢.
推荐答案
按自动完成提示,原型看起来像这样:
Going by what autocomplete suggests the prototype looks like this:
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
显然,除了返回return MKPolylineRenderer()
正常情况下您将返回nil之外,您无能为力.
And apparently there's nothing you can do about it, except for returning return MKPolylineRenderer()
where normally you would return nil.
在我看来,这似乎是一个实现错误,因为这是文档中关于返回对象的内容:
To me it looks like an implementation bug, because here's what the documentation says about the returned object:
我建议您在 Apple的错误报告
这篇关于Swift 2 MKMapViewDelegate rendererForOverlay编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!