プロトコルの汎用配列を使用する方法はありますか? 例えば、Swiftクラスのジェネリックスでプロトコルの配列を使用する
/* I want to use protocol like below,
* but I can't because protocol is not concrete
* so cannot make array of it */
class MyClass<T where T:MyProtocol, T:[MyProtocol]> {
let result: T
}
protocol MyProtocol {
init(with: String)
}
class SpecialThing: MyProtocol {
let appleWatch: AppleWatch
init(with: String) {
self.appleWatch = AppleWatch(with)
}
}
class SampleClass {
func test {
typealias listCallback = (MyClass<[SpecialThing]>, NSError) ->()
typealias oneCallback = (MyClass<SpecialThing>, NSError) ->()
}
}
プロトコルのサブクラスの一つのオブジェクトまたは配列が存在し得ます。 「タタリアス」は私を助けないと思います。
私が何かもっと簡単な方法を見つけたい.....これで
同時にMyProtocol。 – Lachezar
そうだと思います。私はMyClass内の汎用オブジェクトを初期化したい。すべてを研究しましたが、解決策がまだ見つかりません。 – macaron
私が知る限り、これは不可能です。私が知っている唯一の "解決策"は、 'class MyClass { 結果:[T] }' –