0

I want to use an array throughout my view controller so I declare it up top. However, I want populate this array with string elements. There is an easy way to do this with one line. I'm trying not to use a for loop.

var stringArray = [String]()
...
stringArray = Array(reversedWord.characters)

What is is not letting me do is convert Array<_elements> to an array literal.

5
  • 1
    It's not very clear what you're asking. Are you looking for stringArray = reversedWord.characters.map { String($0) }? Commented Jun 8, 2016 at 15:03
  • I'm looking to populate a string with characters. However, I do not know what to declare my initial array as. Commented Jun 8, 2016 at 15:14
  • 1
    Oh, in this case: var charArray = [Character](), then you will be able to do charArray = Array(reversedWord.characters). Commented Jun 8, 2016 at 15:15
  • Just another question, how do you convert a [Character] to a [String] Commented Jun 8, 2016 at 15:21
  • 1
    I've made an answer from my comments, and answered your last question. Commented Jun 8, 2016 at 15:26

1 Answer 1

1

Your issue is that although there can be a String of one character, a single character is actually of type Character.

So when you declare an array of Strings, you can't populate this array later with Characters.

So, make an array of Character types from the beginning:

var charArray = [Character]()

This way, when you'll split a String into characters, you'll be able to add these characters to the array:

charArray += Array(reversedWord.characters)

And to convert an array of Character to an array of String, you can use map and the String() initializer:

let stringArray = charArray.map { String($0) }

Here, stringArray will be of type [String].

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

5 Comments

You don't need the first ` = [Character]()` part. You're making an empty array only to almost instantly replace it with a new onw
@AMomchilov I know. I was just imitating OP's example. But this is a good point, I'm making an edit. Thanks.
@AMomchilov Actually, no, no edit: I did this on purpose. Read the beginning of OP's question: I want to use an array throughout my view controller so I declare it up top. But I fixed a typo (was = instead of += or append). :)
Yes, that would just be an instance variable. You're clobbering it anyway everytime you reassign to it. I think OP was just using that array to let the compiler infer the type of charArray. It just needs a type annotation instead.
Ok, there was a typo I just fixed, I just replaced = with += because that was my original intent (I explain ...able to add... to OP). No clobbering anymore. And no, I won't write just a type annotation here, because I think OP actually uses the array before this. Anyway: implementation detail related to OP's example, and doesn't contradict the explanation about Character != String, which is the heart of the answer: when you declare an array of Strings, you can't populate this array later with Characters. I think this is clear enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.