It is even easier in Swift:
let string : String = "Hello " let characters = Array(string) println(characters) // [H, e, l, l, o, , , , , ]
This uses the facts that
Arraycan be created from a
Stringconforms to the
SequenceTypeprotocol, and its sequence generator enumerates the characters.
And since Swift strings have full support for Unicode, this works even with characters outside of the "Basic Multilingual Plane" (such as ) and with extended grapheme clusters (such as , which is actually composed of two Unicode scalars).
Update: As of Swift 2,
String does no longer conform to
SequenceType, but the
characters property provides a sequence of the
let string = "Hello " let characters = Array(string.characters) print(characters)
This works in Swift 3 as well.
Update: As of Swift 4,
String is (again) a collection of its
let string = "Hello " let characters = Array(string) print(characters) // ["H", "e", "l", "l", "o", " ", "", "", " ", ""]
~ Answered on 2014-09-18 19:58:12