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,
unownedcannot be optional butweakmay benilat some point.From what I understand,
unownedvariable is different from implicitly unwrapping optionals. Implicit unwrapping is for variables, which may benil, but we already know this variable is notnilat the exact point of access. However,unownedvariable can never benilat any point.Thus, you can't use
unownedconstant of typeObserverProtocol!. You need to get rid of!.But if you do get rid of
!,Observerneeds an initializer that initializesresponder.