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.
As you already know,
unowned
cannot be optional butweak
may benil
at some point.From what I understand,
unowned
variable is different from implicitly unwrapping optionals. Implicit unwrapping is for variables, which may benil
, but we already know this variable is notnil
at the exact point of access. However,unowned
variable can never benil
at any point.Thus, you can't use
unowned
constant of typeObserverProtocol!
. You need to get rid of!
.But if you do get rid of
!
,Observer
needs an initializer that initializesresponder
.