swift - 错误 : generic parameter 'Key' could not be inferred when creating a Dictionary

标签 swift swift4

此代码 (1) 出现此错误:

let keys = [1, 1]
let values = ["one", "two"]

let dict = Dictionary(zip(keys, values)){$0 + ", " + $1}

这段代码 (2) 没问题:

let keys = [1, 1]
let values = ["one", "two"]

let dict = Dictionary(zip(keys, values)){$0 + $1}

为什么无法在 (1) 中推断出泛型参数“Key”? (我知道我可以使用 {first, second in first + ", "+ second} 代替,它会起作用;但我只想了解 $0 + 有什么问题", "+ $1$0 + $1 相比)

最佳答案

它似乎无法根据 Int 数组确定字典的键类型。

您需要明确地告诉它 key 的类型。

关于swift - 错误 : generic parameter 'Key' could not be inferred when creating a Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48553435/

相关文章:

ios - 如何显示每个按钮点击不同的文本以制作讲故事的游戏

swift - 键盘文本字段是否取决于文本字段约束?

json - 如何将 Swift 4 编码的 JSON 写入文件?

ios - 符合关联类型协议(protocol)的对象的 Swift 集合

php - Swift 2.0 HTML POST(格式不正确)

xcode - 在 Xcode 6 中观察变量错误

arrays - swift 3 : Check for item in an array

core-data - 使实体关系可编码/可解码时出错

ios - Swift 4.0 中的选择器

ios - 如果按下 "Backspace"按钮并且文本字段为空白并且在 Swift 4 中,我如何移动到上一个 UITextField?