私は同じ機能名を持つ複数のプロトコルを持っています。いくつかのプロトコルには関連するタイプがあります。非汎用プロトコルのように関数を呼び出す方法がわかりません。私はエラーを取得する:Protocol 'MyProtocol1' can only be used as a generic contraint because it has Self or associated type requirements
複数のプロトコル拡張であいまいな機能がありますか?
は、ここで私がやろうとしているものです:
protocol Serviceable {
associatedtype DataType
func get(handler: ([DataType] -> Void)?)
}
struct PostService: Serviceable {
func get(handler: ([String] -> Void)? = nil) {
print("Do something...")
}
}
protocol MyProtocol1: class {
associatedtype ServiceType: Serviceable
var service: ServiceType { get }
}
extension MyProtocol1 {
func didLoad(delegate: Self) {
print("MyProtocol1.didLoad()")
}
}
protocol MyProtocol2: class {
}
extension MyProtocol2 {
func didLoad(delegate: MyProtocol2) {
print("MyProtocol2.didLoad()")
}
}
class MyViewController: UIViewController, MyProtocol1, MyProtocol2 {
let service = PostService()
override func viewDidLoad() {
super.viewDidLoad()
didLoad(self as MyProtocol1) // Error here: Protocol 'MyProtocol1' can only be used as a generic contraint because it has Self or associated type requirements
didLoad(self as MyProtocol2)
}
}
は、どのように私は、特に、一般的なプロトコル拡張から関数を呼び出すことができますか?
確かににおいがします。私はプロトコル拡張を使用して複数の継承を達成しようとしています。例えば、私の 'UIViewController'の' viewDidLoad'では、私の各プロトコルのすべての 'didLoad'関数を実行したいと思います。 – TruMan1
それから、 'let loader:[Loader]'のような、あなたが必要とする様々な初期化を含むプロパティを持つべきです。 –
私はおそらく、現在のスウィフトスタイルのガイドラインと一致させるために、ここの名前についてもっと深く考えると思いますが、それでも私は確かにそれをどのように攻撃するのでしょうか。 –