Adding multiple delegates instead of only one is a quite common task. Suppose we have protocol and a class:
protocol ObserverProtocol
{
...
}
class BroadcasterClass
{
// Error: Type 'ObserverProtocol' does not conform to protocol 'AnyObject'
private var _observers = NSHashTable<ObserverProtocol>.weakObjects()
}
If we try to force ObserverProtocol
to conform AnyObject
protocol, we will get another error:
Using 'ObserverProtocol' as a concrete type conforming to protocol 'AnyObject' is not supported
Is it even possible to create a set of weak delegates in Swift 3.0?
Sure, it's possible.
AnyObject
is the Swift equivalent of id
in Objective C. To get your code to compile, you just need to add the @objc
annotation to your protocol, to tell Swift that the protocol should be compatible with Objective C.
So:
@objc protocol ObserverProtocol {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With