1

I'm trying to specify a class only protocol as generic using Swift 4, but I cannot get it to work. Consider the following code:

protocol SomeProtocol: class {
}

class Bar {
    var foo = Foo<SomeProtocol>()
}

class Foo<Element: AnyObject> {
}

This results in the compiler error: 'SomeProtocol' is not convertible to 'AnyObject'. This should be the case however, since SomeProtocol is a class-only protocol.

Adding AnyObject to the protocol specification also doesn't make a difference.

Werner Altewischer
  • 10,080
  • 4
  • 53
  • 60
  • For what it's worth, if you change it to `` it will compile – TNguyen Nov 07 '17 at 14:07
  • Yes, that's the point, I need to use AnyObject because of a weak var element: Element? which requires that Element needs to be a class (not a struct) – Werner Altewischer Nov 07 '17 at 14:09
  • Wouldn't any implementor of a protocol extending another protocol automatically inherit both? The compiler should know that if SomeProtocol is implemented that automatically AnyObject is implemented as well – Werner Altewischer Nov 07 '17 at 14:10

0 Answers0