Please read the question to the end as it seems to be the duplicate of many similar others but it is not. Most of the other questions use closures with let
keyword to capture weak or unowned self before object init. I do not.
My code:
class Singleton : ObserverProtocol {
static let shared = Singleton()
private let obs : Observer = Observer.init()
private init() { self.obs.responder = self }
func observe(_ object : Any) {}
fileprivate class Observer : NSObject {
unowned var responder : ObserverProtocol!
func observe(_ obj : Any) {
self.responder.observe(obj)
}
}
}
fileprivate protocol ObserverProtocol : class {
func observe(_ object : Any)
}
When I try to compile I get an error highlighted on unowned var responder : ObserverProtocol!
'unowned' may only be applied to class and class-bound protocol types, not 'ObserverProtocol!'
If I change unowned
to weak
I can compile.
There is clearly some concept about unowned
that I don't understand, so I would appreciate if someone could explain it to me.
P.S. I am aware of multiple questions similar to this:
UIView, CMDeviceMotionHandler : unowned may only be applied to class and class-bound protocol types
But I suppose this is not my case.