// want to insert "\n" between characters
var str = "Hello, playground"
var asArray = Array(str) // must first convert to [Character]?
// Cannot do this:
// let vertical1 = asArray.joined(separator: "\n") // Compile error: Referencing instance method 'joined(separator:)' on 'BidirectionalCollection' requires the types 'String.Element' (aka 'Character') and 'String' be equivalent
// There must be a better way?
let vertical2 = asArray.map { "\($0)" }.joined(separator: "\n")
print(vertical2)
[Edit: Sorry for the noise. I totally missâunderstood what you are trying to to.]
I could have sworn a Stringâreturning overload for str.joined(separator:) existed. Maybe I defined it in an extension of my own and forgot.[Edit: It does. See here.]
In any case, you can at least do this:
let vertical = String(str.joined(separator: "\n"))
This is because the generic function returns a special collection that just cleverly iterates out of the original in a different order instead of allocating new space. So the line above manually converts that collection back into a separately allocated String.
I haven't memorized all the initializers, but you may have to add the signature if there are multiple initializers that could match. The one I used can be used for all sequences of a streamable type.