use*_*127 16 protocols ios 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)
想法会有所帮助.
是的,存在差异:(编辑:或者至少在此q的原始版本中,没有添加extension UIView : Draggable {}
到选项1的末尾).
选项1为UIView
符合的实例创建默认实现Draggable
.您仍然需要在声明中标记UIView
您希望遵守Draggable
的内容:class MyView : Draggable
.任何符合Draggable
但不是UIView
子类的东西都需要提供自己的实现.
选项2扩展所有 UIView
s以使它们符合Draggable
.Draggable
除非为这些类编写单独的扩展,否则其他任何内容都不可能,或者它们是手动符合协议的.没有必要Draggable
在类声明中添加.
协议扩展通常是更好的选择.在这种情况下,这显然是正确的,因为不是所有UIView
的都可以Draggable
.此外,沿着协议扩展路由向下意味着你可以创建一个Draggable
不是UIView
子类的对象,如果有必要的话(不可否认,因为大多数Cocoa控件都是UIView
子类 - 尽管不是全部 - UIBarButtonItem
不是,奇怪的是)
如果你遵循选项2,你将UIView
在很多情况下添加不必要的方法,这违反了良好的面向对象设计 - 特别是接口隔离原则(客户端不应该被迫依赖他们不使用的方法) - 这是SOLID原则中的'I'
当您想要为多个类实现功能时,应该使用协议扩展。在这种情况下,您应该使用 ,extension UIView: Draggable
因为该实现特定于 UIView 类。
假设您有一个提供位置的协议:
protocol Location {
var location: CGPoint { get set }
}
Run Code Online (Sandbox Code Playgroud)
并且您希望每个实现 Location 的类都符合 Draggable,那么可以使用协议扩展:
extension Draggable where Self: Location {
func drag() {
}
}
Run Code Online (Sandbox Code Playgroud)
如需进一步参考,您应该查看2015 年 WWDC 上的《Swift 中的面向协议编程》。