1

我有字典:

let alphabet: [Character: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]

let validSet = CharacterSet.init(charactersIn: alphabet.keys.joined())

那就是我得到错误的时候。我该如何解决这个问题,为什么会这样?有任何想法吗?

4

2 回答 2

3

如果您不想像已经建议的那样将密钥类型更改为字符串,那么map将字符更改为字符串:

let alphabet: [Character: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]
let validSet = CharacterSet(charactersIn: alphabet.keys.map{String($0)}.joined())
于 2017-01-31T00:27:56.217 回答
1

您唯一应该更改以使其工作的是键的类型,从CharacterString

let alphabet: [String: Int] = ["a": 0, "b": 1, "c": 2, "d": 3]
let validSet = CharacterSet.init(charactersIn: alphabet.keys.joined())
于 2017-01-31T00:08:59.867 回答