2

How can I convert a string into an array of single strings? So String -> [String]

If I do Array(myString) it results in an array of characters. I tried flatmap (or compactMap in the latest Xcode beta):

stringArray = Array(myString).flatMap { $0 as? String }

Then I get a warning: "Cast from 'Character' to unrelated type 'String' always fails" and `print(stringArray) results in [].

Is this possible?

Edit: this was already answered in a non-accepted answer in this question: Convert Swift string to array

koen
  • 5,383
  • 7
  • 50
  • 89

1 Answers1

7

You can do it like as follows... Initialize a new string inside the map.

let test = "thisIsATest"
let array = test.map( { String($0) })
Harsh
  • 2,852
  • 1
  • 13
  • 27