Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift. unowned may only be applied to class and class-bound protocol types. weak works fine

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.

like image 572
iur Avatar asked Aug 29 '17 13:08

iur


1 Answers

As you already know, unowned cannot be optional but weak may be nil at some point.

From what I understand, unowned variable is different from implicitly unwrapping optionals. Implicit unwrapping is for variables, which may be nil, but we already know this variable is not nil at the exact point of access. However, unowned variable can never be nil at any point.

Thus, you can't use unowned constant of type ObserverProtocol!. You need to get rid of !.

But if you do get rid of !, Observer needs an initializer that initializes responder.

like image 170
sCha Avatar answered Nov 16 '22 03:11

sCha