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 assume that now any item the conforms to PinProtocol as essentially the same as being an MKAnnotation... just more!
protocol PinProtocol: MKAnnotation {
// stuff
}
I have 2 Classes, Staff and Clients
Both conform to PinProtocol (which then also needs to conform to MKAnnotation)
I know this is working as if I set up my class as such,
class Client: NSObject, PinProtocol {
//var coordinate:CLLocationCoordinate2D // Leave out get doesn't conform to protocol warning as expected
}
So, this tells me that that PinProtocol is working as expected, as items that need to adhere to the PinProtocol are also conforming to the MKAnnotation protocol as well. Because coordinate:CLLocationCoordinate2D, is required by MKAnnotation.
So why do I get this issue with
let staffAndClients = [PinProtocol]()
mapView.addAnnotations(staffAndClients) // not allowed!
//mapView.addAnnotations(pins as! [MKAnnotation]) // also not allowed
The error is, cannot convert value of type [PinProtocol] to to expected argument [MKAnnotation]
Isn't PinProtocol conforming to MKAnnotation so should work.
But doing this works fine
let staff = [Staff]()
mapView.addAnnotations(staff) // no problem
let clients = [Client]()
mapView.addAnnotations(clients) // no problem
I can get around the issue using AnyObject, but why cannot I use PinProtocol - which to me seems cleaner and the whole idea of protocol extensions.
Thanks for any help.
Addit...
The way I am getting around it for those who are facing a similar issue is
var pins = [AnyObject]()
mapView.addAnnotations(pins as! [MKAnnotation])