8
votes

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.

2
Not a duplicate. I am not asking how to make a weak protocol reference, I listed example on how that's done.Boon
I don't think you can achieve that with a single protocol which can be adopted by both classes and structs. But you could defined a subprotocol protocol MyClassDelegate : class, MyDelegate { } and then weak var delegate : MyClassDelegate?.Martin R
it has no sense to have weak reference to value type instance, hasn't it?user3441734
@user3441734 It doesn't - therefore regular delegate protocol cannot enforce retain cycle per the current way of things even though it allows class conformance, that's against Swift safety philosophy.Boon
Why would you want a delegate to be struct?0x416e746f6e

2 Answers

2
votes

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)")
1
votes

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.