私が制御していないライブラリを扱っていて、コールバック型を定義するクラスをラップして、テスト目的でコードを分離しようとしているとしましょう。module 内のクラスは次の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 {}
タイプ 'Application'がプロトコル 'ApplicationProtocol' に準拠していません。UIElementProtocol
ApplicationProtocol コールバック内を に戻すと、UIElement
機能します。しかし、にUIElement
準拠してUIElementProtocol
いるのに、なぜこれが機能しないのですか?
2 番目の質問: ライブラリ API を設計して、この種のことを可能にするより良い方法はありますか?