Ich bin versucht, ein erzwungenes Protokoll-Erweiterung zu einer Struktur (Swift 2.0) und Empfangen der folgenden Compiler-Fehler anwenden:Protokollerweiterungen auf Structs Ursachen kompilieren Fehler ‚Self‘ beschränkt auf nicht-Protokolltyp
type 'Self' constrained to non-protocol type 'Foo'
struct Foo: MyProtocol {
let myVar: String
init(myVar: String) {
self.myVar = myVar
}
}
protocol MyProtocol {
func bar()
}
extension MyProtocol where Self: Foo {
func bar() {
print(myVar)
}
}
let foo = Foo(myVar: "Hello, Protocol")
foo.bar()
Ich kann diesen Fehler beheben, indem ich struct Foo
zu class Foo
ändere, aber ich verstehe nicht, warum das funktioniert. Warum kann ich nicht ein where Self:
Constrained-Protokoll eine Struktur machen?
Nun, das ist dann ärgerlich. Danke für die Antwort! – Benjohn