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

标签 swift types

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/

相关文章:

swift - 以编程方式滚动到 NSScrollView 末尾时出现问题

c - Go 的 interface{} 和 C 中的 void* 一样吗?

encoding - 用于存储纬度和经度的正确/最佳类型

c# - 在 C# 中调用类型的每个实例的方法

.net - 将 "obj"向下转换为底层数组类型(不带类型注释)

python - 如何检查 Python 中的函数类型?

SwiftUI @FetchRequest 使应用程序崩溃并返回错误

Swift - UISwitch 作为两个值之间的选择器?

ios - 如何处理完成处理程序 block 中已取消的 NSURLSessionTask?

ios - 在 UITableView 的单元格中方形 UImageView