是否可以执行以下操作:
protocol A: class {
typealias T: AnyObject
}
extension A {
func testA(a:Self, _ t:T)->Void{
print(a, t)
}
}
class B:A {
typealias T = String
}
换句话说,我有协议(protocol)并且想在符合它的类中提供具体类型。
最佳答案
应该没问题。您的代码的唯一问题是 String
不是 AnyObject
。
你只需要:
protocol A: class {
typealias T
}
extension A {
func testA(a:Self, _ t:T)->Void{
print(a, t)
}
}
class B:A {
typealias T = String
}
关于swift - 为通用协议(protocol)实现提供具体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36059308/