If you're doing this for a user-entered string, don't worry about how they're encoded.
CNPhoneNumber.stringValue
is of type String
A String
is a collection of Characters
You could iterate through the String by hand, but it is awkward. Swift offers compactMap, which will give you an array of single-character strings. A new String can be created from this Array.
for example
let likeAPhoneNumber = "(451)∕234-141😃0"
let newNumberArray = likeAPhoneNumber.compactMap { $0.isNumber ? $0 : nil }
let newNumber = String(newNumberArray)
print (newNumber)prints 4512341410
None of this is high-performance, because iterating through a Unicode string is non-trivial.