mokacoding

unit and acceptance testing, automation, productivity

Swift array of characters from String

Sometimes you might need to get an array of characters out of a String, for example:

"foo bar" => ["f", "o", "o", " ", "b", "a", "r"]

To do so you can do:

"foo bar".map { String($0) }

or if you are into point free notation:

"foo bar".map(String.init)

There you go. 😎


For the record, this post dates back to Swift version 2. If you're into programming language archeology, checkout the original version here.

Want more of these posts?

Subscribe to receive new posts in your inbox.