I already tried this:
var newArray = oldArray.map
{
Character($0)
}
where oldArray is an array with type [Any]. However, I get an error when I insert that code. Is there another option?
Any cannot be directly converted to Character, but needs to first be casted to String (from which it is a simple task to convert (the first character in the string) to Character:
let arr: [Any] = ["k", "n"]
let newArr = arr.flatMap { ($0 as? String)?.characters.first }
print(newArr, type(of: newArr)) // ["k", "n"] Array<Character>
If oldArray contains object of type Character:
var oldArray:[Any] = []
....
var newArray = oldArray.map {
$0 as! Character
}
in this way it compiles, but you can get runtime exception accordin to the value stored in oldArray.
oldArray aren't Characters (or can't be converted to Characters). And if they are already Characters, then there's no need for them to be converted.
oldArrayan array of?