事实证明,您只需要指定一个
@objc protocol
所有其他协议(也必须是
@objc协议
s) 符合,例如:
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!