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] } –