1

Seeing what I believe is a bug in Swift with a generic based protocol. The protocol is below:

protocol Coordinated {
    associatedtype GenericCoordinatingDelegate: CoordinatingDelegate
    weak var coordinatingDelegate: GenericCoordinatingDelegate? { get set }
}

protocol CoordinatingDelegate: NSObjectProtocol { }

Xcode shows an error with this, however, claiming that 'weak' may only be applied to class and class-bound protocol types, not Self.GenericCoordinatingDelegate.

Error

Now, correct me if I'm wrong, but GenericCoordinatingDelegate is here constrained to the CoordinatingDelegate protocol type defined below, which is derived from NSObjectProtocol, hence constraining it to classes only. Swift can see this and therefore should allow the weak modifier to be used on the property derived from it.

Can anyone see anything that I've missed or is this actually a bug in Swift?

EDIT:

Thank you for your suggestion, Hamish, I have edited my code to reflect this. However, I am now getting another strange issue.

class WelcomeIntroViewController: UIViewController, OnboardingViewController, TwoPartGradientLayerProvider, Coordinated {

    typealias GenericCoordinatingDelegate = WelcomeCoordinatingDelegate
    weak var coordinatingDelegate: GenericCoordinatingDelegate?

That is the implementation of the protocol as defined above, however, Swift claims that the view controller does not conform to the protocol, and tries to add another typealias. It will do this continuously, even with like 50 matching typealiases. Not sure what's going on here?

Jacob King
  • 6,025
  • 4
  • 27
  • 45
  • 2
    `= CoordinatingDelegate` provides the associated type with a default type, but it doesn't place any constraints on it. You would have wanted to say `associatedtype GenericCoordinatingDelegate : AnyObject = CoordinatingDelegate` or perhaps more likely what you were intending `associatedtype GenericCoordinatingDelegate : CoordinatingDelegate`. But note that the `weak` attribute in protocols has no effect anyway, compare https://stackoverflow.com/q/47699813/2976878. – Hamish Apr 09 '18 at 10:19
  • @Hamish would you mind speculating on my edit? – Jacob King Apr 09 '18 at 10:23
  • What's `WelcomeCoordinatingDelegate`? Presumably it's a protocol that derives from `CoordinatingDelegate`? If that's the case, then you've run into the age old problem of [protocols not conforming to themselves](https://stackoverflow.com/questions/33112559/protocol-doesnt-conform-to-itself/43408193#43408193) (i.e `WelcomeCoordinatingDelegate` doesn't conform to `CoordinatingDelegate` so cannot satisfy the associated type). – Hamish Apr 09 '18 at 10:26
  • It's exactly that, yes. Thanks for the article, that was an interesting read and definitely provided some insight into the problem. I have managed to workaround the issue by annotating my protocols as `@objc`. Obviously this is not ideal, but it will do for now. By the sounds of things, they're working on improving the way Swift handles this anyway. – Jacob King Apr 09 '18 at 10:39

0 Answers0