假设我正在处理一个我无法控制的库,并且我正在尝试包装一个定义回调类型的类,以解耦我的代码以进行测试。这是模块内部的类AXSwift
:
public class Application {
public typealias Callback = (element: UIElement) -> ()
public func createObserver(callback: Callback) -> Observer? {
// ...
}
}
这是用于测试的包装协议:
protocol UIElementProtocol {}
extension AXSwift.UIElement: UIElementProtocol {}
protocol ApplicationProtocol {
func createObserver(callback: (element: UIElementProtocol) -> ()) -> Observer?
}
extension AXSwift.Application: ApplicationProtocol {}
我得到Type 'Application' does not conform to protocol 'ApplicationProtocol'。如果我UIElementProtocol
在 ApplicationProtocol 回调中更改回UIElement
,它可以工作。但是UIElement
符合UIElementProtocol
,那么为什么这不起作用呢?
第二个问题:有没有更好的方法来设计库 API 以允许这种事情?