How can I “explicitly” implement a protocol in swift? If it is impossible, why?

前端 未结 1 1666
[愿得一人]
[愿得一人] 2021-01-14 08:33

In C#, there is this great language feature called \"explicit interface implementations\" that allows you to implement two or more interfaces where the names of the interfac

1条回答
  •  抹茶落季
    2021-01-14 09:18

    Protocol extensions basically already do what you're describing:

    protocol Cat {
    }
    extension Cat {
        func quack() {
            print("meow")
        }
    }
    class Duck : Cat {
        func quack() {
            print("quack")
        }
    }
    let d = Duck()
    d.quack() // quack
    (d as Cat).quack() // meow
    

    0 讨论(0)
提交回复
热议问题