is anyone able to tell me why this does not work in Swift 3? I have a protocol which conforms to AnyObject like this:
protocol EventListenerProtocol: AnyObject {
func doSomething()
}
then I have an emitter protocol with default implementation like this:
protocol EventEmitterProtocol {
associatedtype ListenerType
var listeners: [ListenerType] { get set }
func addEventListener(_ listener: ListenerType)
func removeEventListener(_ listener: ListenerType)
}
extension EventEmitterProtocol where ListenerType: AnyObject {
... funcs implemented ...
}
removeEventListener requires ListenerType to be AnyObject to be able to use identity operator.
After all that, I'm trying to use this in my class:
class myClass: EventEmitterProtocol {
var listeners: [EventListenerProtocol] = []
}
and it says my class does not conform to EventEmitterProtocol. When I change EventListenerProtocol to AnyObject as the array type, it does compile. EventListenerProtocol conforms to AnyObject, so why the error pops up? I'd be really grateful for any help :)