2
有什麼辦法可以使用協議的泛型數組嗎? 例如,在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) ->()
}
}
可以有一個對象或協議的子類的數組。 我認爲「typealias」不幫助我。
我想找到更簡單的方法.....
,我不認爲這是可能有通用類型T指定爲MyProtocol和數組MyProtocol在同一時間。 – Lachezar
我這麼認爲。我想在MyClass中初始化通用對象。研究一切,但仍然無法找到解決方案。 – macaron
不可能,因爲我知道。很明顯,但我知道的唯一「解決方案」是class MyClass讓結果:[T] } –