我有字典:
let alphabet: [Character: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]
let validSet = CharacterSet.init(charactersIn: alphabet.keys.joined())
那就是我得到错误的时候。我该如何解决这个问题,为什么会这样?有任何想法吗?
我有字典:
let alphabet: [Character: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]
let validSet = CharacterSet.init(charactersIn: alphabet.keys.joined())
那就是我得到错误的时候。我该如何解决这个问题,为什么会这样?有任何想法吗?
如果您不想像已经建议的那样将密钥类型更改为字符串,那么map
将字符更改为字符串:
let alphabet: [Character: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]
let validSet = CharacterSet(charactersIn: alphabet.keys.map{String($0)}.joined())
您唯一应该更改以使其工作的是键的类型,从Character
到String
:
let alphabet: [String: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]
let validSet = CharacterSet.init(charactersIn: alphabet.keys.joined())