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?

1

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.