Colin Colin - 1 year ago 96
Swift Question

Does swift inheritance from generic class prevent protocol conformance via extension?

@objc public protocol P1 {
func p1foo()

@objc public protocol P2 {
func p2foo()

class A<T>: NSObject, P1 {
func p1foo() { }

class B: A<Int> {


extension B: P2 {
func p2foo() { }

-> Type 'B' does not conform to protocol 'P2'.

Why? Removing the generic solves this error, but I don't understand - B should be a fully-specific type at this point...

Answer Source

It's the combination of the @objc and the generic. Objective-C knows nothing of Swift generics, so your notion that B should adopt an @objc protocol causes the compiler to throw a wobbly.

You've already detected this from one direction; and you can equally see it from the other. You can make your code compile by deleting the @objc attributes. Or you can make it compile by removing the generic. You can't have both at once.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download