gpt4 book ai didi

swift - 在结构内指定 Hashable 类型约束

转载 作者:行者123 更新时间:2023-11-30 10:48:22 28 4
gpt4 key购买 nike

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com