小编use*_*127的帖子

协议扩展与Swift中的类扩展

假设有一个协议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)

这是我的问题:

  • 这两种方法(选项1和选项2)之间是否存在差异?
  • 如果是,那么我们设计项目或图书馆时有什么区别以及如何选择?

想法会有所帮助.

protocols ios swift

16
推荐指数
2
解决办法
2410
查看次数

标签 统计

ios ×1

protocols ×1

swift ×1