Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Protocol as Generic Parameter to Class

I am trying to create a class (ProtocolPrinter) that takes a protocol as a generic parameter. Why won't this compile?

import Foundation

@objc protocol MyProtocol {
    func foo()
}

class MyConformingClass: MyProtocol {
    func foo() {
        print("Foo!")
    }
}

class ProtocolPrinter<T: Protocol> {
    func printT() {
        print("T: \(T.self)")
    }

    func dosomethingWithObject(_ object: T) {
        if let object = object as? MyProtocol {
            object.foo()
        } else {
            print("I don't know what object this is: \(object).")
        }
    }
}

let x = MyConformingClass()
x.foo() // Foo!

let myProtocolMeta: Protocol = MyProtocol.self // No error.

ProtocolPrinter<MyProtocol>()                        // 'ProtocolPrinter' requires that 'MyProtocol' inherit from 'Protocol'
ProtocolPrinter<MyProtocol.self>()                   // (treats "<" and ">" as operators)
ProtocolPrinter<MyProtocol.Type>()                   // 'ProtocolPrinter' requires that 'MyProtocol.Type' inherit from 'Protocol'
ProtocolPrinter<MyProtocol.Type.self>()              // (treats "<" and ">" as operators)
ProtocolPrinter<MyProtocol.Protocol>()               // type 'MyProtocol.Protocol' does not confor
ProtocolPrinter<MyProtocol.Protocol.self>()          // (treats "<" and ">" as operators)
ProtocolPrinter<MyProtocol.Protocol.Type>()          // type 'MyProtocol.Protocol.Type' does not conform to protocol 'MyProtocol'
ProtocolPrinter<MyProtocol.Protocol.Type.self>()     // (treats "<" and ">" as operators)
ProtocolPrinter<MyProtocol.Protocol.Protocol>()      // cannot use 'Protocol' with non-protocol type 'MyProtocol.Protocol'
ProtocolPrinter<MyProtocol.Protocol.Protocol.Type>() // cannot use 'Protocol' with non-protocol type 'MyProtocol.Protocol'
like image 312
Coder-256 Avatar asked Dec 05 '25 18:12

Coder-256


1 Answers

It turns out that all you need to specify a single @objc protocol that all of your other protocols (which must also be @objc protocols) conform to, for example:

import Foundation

@objc protocol SuperProtocol {}
@objc protocol MyProtocol: SuperProtocol {
    func foo()
}

class MyConformingClass: MyProtocol {
    func foo() {
        print("Foo!")
    }
}

class ProtocolPrinter<T: SuperProtocol> {
    func printT() {
        print("T: \(T.self)")
    }

    func dosomethingWithObject(_ object: T) {
        if let object = object as? MyProtocol {
            object.foo()
        } else {
            print("I don't know what object this is: \(object).")
        }
    }
}

let x = MyConformingClass()
x.foo() // Foo!
MyProtocol.Protocol.self
let myProtocolMeta: Protocol = MyProtocol.self

ProtocolPrinter<MyProtocol>().dosomethingWithObject(MyConformingClass()) // Foo!
like image 74
Coder-256 Avatar answered Dec 07 '25 13:12

Coder-256