【发布时间】:2019-03-09 22:57:27
【问题描述】:
我有这个协议
protocol BinaryTreeProtocol {
associatedtype T
var info: T { get set }
var left: Self? {get set}
var right: Self? {get set}
func addItem(item: T)
}
而且我无法完成它的实现。
struct BinaryTreeImplementation: BinaryTreeProtocol {
typealias T = Int
var info: Int
var left: BinaryTreeImplementation? // There is an error here.
var right: BinaryTreeImplementation? // how can I sort it?
func addItem(item: Int) {
}
}
【问题讨论】:
-
那么你不能拥有一个包含自身的属性
-
@LeoDabus 即使是可选的?
-
没什么区别
-
使用类而不是结构。
标签: swift generics swift-protocols type-alias associated-types