假设有一个协议Draggable,通常会被一个UIView对象
所遵循
protocol Draggable {
drag()
}
Run Code Online (Sandbox Code Playgroud)
我们可以drag()在协议扩展中实现option 1
// option 1
extension Draggable where Self: UIView {
func drag() {
// implementation
}
}
extension UIView: Draggable {} // added after @Rich Tolley's answer
Run Code Online (Sandbox Code Playgroud)
或者我们可以drag()在UIView扩展中实现option 2
// option 2
extension UIView: Draggable {
func drag() {
// implementation
}
}
Run Code Online (Sandbox Code Playgroud)
想法会有所帮助.