I'm trying to filter non-alphabetical characters out of a String, but running into the issue that CharacterSet
uses Unicode.Scalar
and String consists of Character
.
Xcode gives the error:
Cannot convert value of type 'String.Element' (aka 'Character') to specified type '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
}