Swift delegate protocol cannot prevent retain cycle issue

♀尐吖头ヾ 提交于 2019-12-12 10:46:20

问题


In Swift, if I create a delegate protocol, it can be conformed to by class and struct.

protocol MyDelegate {
  // Can be conformed to by class or struct
}

The issue comes up when I declare the delegate. If the delegate is a class instance, I want the variable to be weak to avoid retain cycle. If it is a struct, there is no such need - in fact, Swift won't allow me to make the delegate variable weak. Note: I know how to create a weak delegate, but the key question is - if you create a delegate protocol that can be weak, unless you make it class-conforming only, you cannot enforce retain cycle.

class MyClass {
  // Want weak var here to avoid cyclical reference
  // but Swift won't allow it because MyDelegate can be
  // conformed by struct as well.  Dropping weak means
  // cyclical reference cannot be prevented
  weak var delegate: MyDelegate?
}

class MyConformingClass: MyDelegate {

}

or

struct MyConformingStruct: MyDelegate {

}

It seems like we need to declare the protocol to be for class only at all times like this because a non regular delegate protocol cannot prevent retain cycles:

protocol MyDelegate: class {

}

The fact that Swift allows you to shoot yourself in the foot this way seems to go against its design philosophy in safety.


回答1:


It takes two to have a retain cycle...

If you really want to do it this way, then why not leave out the weak and just make it a strong reference. You would only have a problem if the delegate also maintained a strong reference to the object that it was delegating for.

So it becomes the duty of the delegate to make sure any reciprocal references are weak, which should be possible all of the time since MyClass is a class, and therefore you can always declare a weak reference to it.




回答2:


If you really want to support a protocol on a class or struct you can always store the delegate in separate underlying variables. That way you can have one weak for when the delegate is a class. Along the lines of the following:

protocol MyDelegate { // Can be conformed to by class or struct }

class MyClass {
    private weak var delegateFromClass: AnyObject?
    private var delegateFromStruct: MyDelegate?
    var delegate: MyDelegate? {
        get {
            return (delegateFromClass as? MyDelegate) ?? delegateFromStruct
        }
        set {
            if newValue is AnyObject {
                delegateFromClass = newValue as? AnyObject
                delegateFromStruct = nil
            } else {
                delegateFromClass = nil
                delegateFromStruct = newValue
            }
        }
    }
}

class MyConformingClass: MyDelegate {

}

struct MyConformingStruct: MyDelegate {

}

print(" \(MyConformingClass() is AnyObject) \(MyConformingStruct() is AnyObject)")


来源:https://stackoverflow.com/questions/34493644/swift-delegate-protocol-cannot-prevent-retain-cycle-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!