1

我升级到更新的 Swift 编译器并遇到了这个编译器错误,我不知道如何解决。我有一堆从 MKMapViewDelegate 声明的 mapView 函数。除了引发此错误的这个之外,它们似乎都匹配:

ViewController.swift:137:10:Objective-C 方法 'mapView:viewForAnnotation:' 提供​​的方法 'mapView( :viewForAnnotation:)' 与协议 'MKMapViewDelegate' 中的可选要求方法 'mapView( :viewForAnnotation:)' 冲突</p >

ViewController.swift:12:7:“ViewController”类在此处声明符合协议“MKMapViewDelegate”

/ViewController.swift:137:10: 此处声明的需求'mapView(_:viewForAnnotation:)' (MapKit.MKMapViewDelegate)

我查看了 MKMapViewDelegate 方法,我认为我正确匹配了它,所以我对需要更正的更改感到困惑。从那里我看到

可选 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!

这是我下面的声明,它会引发错误。

    class ViewController: UIViewController, MKMapViewDelegate {

        func mapView(aMapView: MKMapView!, viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView! {
         //The error is thrown here on the m in mapView
        }
    }
4

1 回答 1

2

我查看了 MKMapViewDelegate 方法,我认为我正确匹配了它,所以我对需要更正的更改感到困惑。从那里我看到

optional func mapView(mapView: MKMapView!, 
    viewForAnnotation annotation: MKAnnotation!) 
    -> MKAnnotationView!

真的吗?您认为您的声明与此相符吗?仔细看。你有:

func mapView(aMapView: MKMapView!, 
    viewForAnnotation annotation: CustomMapPinAnnotation!) 
    -> MKAnnotationView! {

像这样重写它:

func mapView(aMapView: MKMapView!, 
    viewForAnnotation annotation: MKAnnotation!)
    -> MKAnnotationView! {

你看得到差别吗?您不能更改委托方法声明中的类型。声明有MKAnnotation,你必须声明MKAnnotation。此 MKAnnotation 可能也可能不是 CustomMapPinAnnotation,但这是由您在函数体内的代码决定的。

于 2015-05-06T14:39:30.057 回答