此代码 (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/