protocol Component {}
struct Container {
let map: [Component: Component]
}
在上面的代码中,如何指定 map
的键可以包含任何类型的 Component
并且也是 Hashable
?
最佳答案
尝试代码
protocol Component {}
struct Container<T: Hashable & Component> {
let map: [T: Component]
}
或者
protocol Component {}
struct Container<T> where T: Hashable, T: Component {
let map: [T: Component]
}
关于swift - 在结构内指定 Hashable 类型约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290386/