I need to store an array of weak referenced protocols:
class Weak<T: AnyObject> {
weak var value: T?
init (_ value: T) {
self.value = value
}
}
protocol LocationSendingDelegate : AnyObject {
func sendLocation(_ location: CLLocation)
}
private var locationSendingDelegates : [Weak<LocationSendingDelegate>] = [] // this line gives compiler error
The last line gives a compiler error:
'Weak' requires that 'any LocationSendingDelegate' be a class type
If I get rid of generics and declare:
class WeakLocationDelegate {
weak var value: LocationSendingDelegate?
init(_ value: LocationSendingDelegate) {
self.value = value
}
}
Then, this works fine:
private var locationSendingDelegates : [WeakLocationDelegate] = []
How can I do this with generics?
Someone else ran into this issue too but it wasn't resolved. This is not a duplicate. That question is about an array of weak classes whereas mine is about an array of weak protocols. Obviously the error I am getting about "class type" wouldn't happen if I were using class and not protocol.
weak var locationSendingDelegates : [any LocationSendingDelegate] = []instead. Is there something we're missing here?