【问题标题】:Specifying a Hashable type constraint inside a struct在结构内指定 Hashable 类型约束
【发布时间】:2019-08-12 21:29:36
【问题描述】:
protocol Component {}

struct Container {
    let map: [Component: Component]
}

在上面的代码中,如何指定map 的键可以包含任何类型的Components 也是Hashable

【问题讨论】:

    标签: swift types


    【解决方案1】:

    试试代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      相关资源
      最近更新 更多