Conform to Protocol MKAnnotation via Custom Protocol

后端 未结 2 1771
星月不相逢
星月不相逢 2021-01-14 05:25

I want to get 2 or more different object types on a map easily. Swift 2.0, I wanted to use Protocols.

I created a protocol that these object need to conform too. I a

2条回答
  •  猫巷女王i
    2021-01-14 06:09

    After dealing with the hassle of MapViewController's data source being

    var pins = [AnyObject]()
    

    I came to the realisation that I could easily avoid all the associated issues of type checking and use the preferred array of PinProtocols

    var pins = [PinProtocol]()
    

    The workaround was simply

    func addToMap() {
        let mappingPins = pins.map{ $0 as AnyObject }
        mapView.addAnnotations(mappingPins as! [MKAnnotation])
        mapView.showAnnotations(mappingPins as! [MKAnnotation], animated: true)
    }
    

提交回复
热议问题