我试图做一个元组类型有一个Hashable项目&任何项目,&在字典的自定义初始化程序中使用它。但是,Swift不会以这种方式使用Hashable,&我被卡住了。我不想将我可以创建的字典限制为仅使用字符串作为键。元组类型与可哈希项目
protocol CollectionInitializeable {
associatedtype T
init(items: [T])
}
extension Dictionary: CollectionInitializeable {
typealias T = (Hashable, Any) // not allowed
init(items: [T]) {
self.init()
// etc...
}
}
'CollectionInitializeable'但初始化程序需要一个'Array',而不是'Collection'。 – Alexander
@Alexander Point重新命名,也许我应该调用协议ArrayInitializeable :) - 但我没有从这个代码示例没有给出任何迹象,为什么我已经实现了它 - 我不认为这是这里的问题。如果我选择使用元组数组初始化字典,那就是我的电话号码... – SomaMan
更新:由于['Dictionary.init(uniqueKeysWithValues:)'](https://developer.apple .com/documentation/swift/dictionary/2894798-init)和['Dictionary.init(_:uniquingKeysWith:)'](https://developer.apple.com/documentation/swift/dictionary/2892961-init) – Alexander