我正在尝试从字符串中过滤出非字母字符,但遇到了CharacterSet使用Unicode.Scalar和字符串组成的问题Character.
Xcode 给出了错误:
无法将“String.Element”(又名“Character”)类型的值转换为指定类型“Unicode.Scalar”?
let name = "name"
let allowedCharacters = CharacterSet.alphanumerics
let filteredName = name.filter { (c) -> Bool in
if let s: Unicode.Scalar = c { // cannot convert
return !allowedCharacters.contains(s)
}
return true
}
Run Code Online (Sandbox Code Playgroud)