Convert a String to an array of characters swift 2.0 Convert a String to an array of characters swift 2.0 swift swift

Convert a String to an array of characters swift 2.0


var myString = "Hello"let characters = [Character](myString.characters)  // ["H","e","l","l","o"]

Hope this helps


First, use the characters property of String struct :

let str = "Hello World"var charView = str.characters

You get an CharacterView instance. To access to an element of charView, you have to use String.CharacterView.Index. If you want to convert this to an array of String, do this :

let str = "Hello World"var arr = str.characters.map { String($0) }

Now, you have an array of type [String] :

arr[0] // => "H"


You have to use the characters property of String since it is no longer a SequenceType:

var myString = "Hello"let charactersArray = Array(myString.characters)