protocol TestProtocol {
associatedtype TestElement
func run(_ param: TestElement)
}
class TestObject: TestProtocol {
typealias TestElement = Int
func run(_ param: Int) {
if param == 1 {
print("run")
}else {
print("wrong number")
}
}
}
struct TestStruct: TestProtocol {
typealias TestElement = String
func run(_ param: String) {
if param == "asd" {
print("run")
}else {
print("wrong string")
}
}
}
let t = TestObject()
t.run(1)
let t2 = TestStruct()
t2.run("wer")
Swift泛型在协议中的使用
最新推荐文章于 2022-08-19 00:04:45 发布