Swift汎用プロトコルでの使用
676 ワード
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")