0

I am trying to split a string into individual characters. The string I want to split: let lastName = "Kocsis" so that is returns something like: ["K","o","c","s","i","s"]

So far I have tried:

  1. var name = lastName.componentsSeparatedByString("") This returns the original string

  2. name = lastName.characters.split{$0 == ""}.map(String.init) This gives me an error: Missing argument for parameter #1 in call. So basically it does't accept "" as an argument.

  3. name = Array(lastName) This does't work in Swift2

  4. name = Array(arrayLiteral: lastName) This doesn't do anything.

How should I do this? Is There a simple solution?

Kocsis Kristof
  • 74
  • 2
  • 10
  • 1
    Possible duplicate of [Convert Swift string to array](http://stackoverflow.com/questions/25921204/convert-swift-string-to-array) – vadian Jun 11 '16 at 13:47

1 Answers1

3

Yes, there is a simple solution

let lastName = "Kocsis"
let name = Array(lastName.characters)

The creation of a new array is necessary because characters returns String.CharacterView, not [String]

vadian
  • 274,689
  • 30
  • 353
  • 361