找到你要的答案

Q:Type cast class conforming to sub-protocol to parent-protocol in Swift

Q:快速符合子协议到父协议的类型

I'm having a hard time getting an MKMapView subclass to work:

The subclass provides a clustering functionality. Because annotations are being moved to a cluster with a nice animation, the annotations provided to the map must have a setter for coordinate. Hence, I've created a 'sub-protocol' of MKAnnotation:

protocol MyAnnotationProtocol: MKAnnotation {
    var coordinate: CLLocationCoordinate2D { get set }
}

So internally the subclass of MKMapView works with objects conforming to MyAnnotationProtocol.

The clustering algorithm itself is outsourced to another class. For the sake of reusability, the algorithm itself works with MKAnnotation objects - no need to provide MyAnnotationProtocol objects.

The concrete class I'm providing as annotations to the map looks like this:

class MXPointAnnotation: NSObject, MyAnnotationProtocol {
    var coordinate: CLLocationCoordinate2D

    override init() {
        coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
    }
}

When running, the app crashes at this line inside the cluster algorithm class:

let seeds = regionQueryForAnnotation(annotation as! MKAnnotation, inMapView: mapView)

annotation is stored in an NSSet - therefore it has to be cast before it can be used.

The log says:

Could not cast value of type 'NSKVONotifying_********.MXPointAnnotation' (0x13402cf40) to 'MKAnnotation' (0x1a1d89ec8).

******** is my apps name.

This seems like a bug to me, because clearly MXPointAnnotation conforms to MyAnnotationProtocol and therefore to MKAnnotation?

What really drives me crazy is the fact that when I'm changing the interface of MXPointAnnotation to:

class MXPointAnnotation: NSObject, MKAnnotation, MyAnnotationProtocol {
....
}

the app does NOT crash and everything works as expected.

Note that the protocol conformance is redundant for MKAnnotation, because MyAnnotationProtocol already conforms to that.

Is this a current Swift bug or am I doing something wrong here?

我有一个很难获得一个MKMapView类工作:

子类提供了聚类功能。因为注释被转移到一个好的动漫产业集群,注释提供的地图必须有一个二传手的坐标。因此,我创建了一个“子协议”MKAnnotation中:

protocol MyAnnotationProtocol: MKAnnotation {
    var coordinate: CLLocationCoordinate2D { get set }
}

所以在作品符合myannotationprotocol MKMapView对象类。

聚类算法本身被外包到另一类。为可重用的缘故,算法本身的工作对象——MKAnnotation中不需要提供myannotationprotocol对象。

我给地图提供的具体类看起来像这样:

class MXPointAnnotation: NSObject, MyAnnotationProtocol {
    var coordinate: CLLocationCoordinate2D

    override init() {
        coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
    }
}

运行时,应用程序崩溃在这一行内的集群算法类:

let seeds = regionQueryForAnnotation(annotation as! MKAnnotation, inMapView: mapView)

注释存储在一个NSSet -因此它必须才可以使用。

日志说:

Could not cast value of type 'NSKVONotifying_********.MXPointAnnotation' (0x13402cf40) to 'MKAnnotation' (0x1a1d89ec8).

********是我的应用程序名称。

这似乎是一个错误的我,因为mxpointannotation符合myannotationprotocol因此MKAnnotation中吗?

真正让我疯狂的是,当我改变mxpointannotation接口:

class MXPointAnnotation: NSObject, MKAnnotation, MyAnnotationProtocol {
....
}

the app does NOT crash and everything works as expected.

Note that the protocol conformance is redundant for MKAnnotation, because MyAnnotationProtocol already conforms to that.

Is this a current Swift bug or am I doing something wrong here?

ios  swift  mkmapview  mkannotation