0

I have 2 protocols and a class that conforms to it.

protocol SomeProtocol: AnyObject { }

protocol AnotherProtocol: AnyObject { }

protocol HelperProtocol {
  var delegate: AnotherProtocol? { get }
}

class SomeClass {
  weak var delegate: (SomeProtocol & AnotherProtocol)?
}

extension SomeClass: HelperProtocol { // Type 'SomeClass' does not conform to protocol 'HelperProtocol'

}

How do I fix the compile error?

iOSer
  • 235
  • 2
  • 14
  • Try replacing ```var delegate: AnotherProtocol?``` to ```var delegate: (SomeProtocol & AnotherProtocol)?``` – 0-1 Jun 08 '20 at 23:07

1 Answers1

0

There's no fundamental reason why Swift can't support this, but it just doesn't (as of now). This answer does a good job explaining.

The best solution I think, while unfortunately ugly, is to make two properties.

class SomeClass: HelperProtocol {
  weak var strictlyTypedDelegate: (SomeProtocol & AnotherProtocol)?
  var delegate: AnotherProtocol? { strictlyTypedDelegate }
}
Jamie A
  • 881
  • 1
  • 6
  • 14