-1

How do i Print a shuffled array without brackets and commas in swift? For example if I create an array:

var names = ["john", "connor", "Sarra"]
print(names.shuffled())

It will print the names array with commas and brackets - I want it to be printed without anything !

0

1 Answer 1

0

You can use the joined(separator:) method:

var names = ["john", "connor", "Sarra"]
print(names.shuffled()) // ["john", "connor", "Sarra"]
print(names.shuffled().joined(separator: "")) // connorSarrajohn
print(names.shuffled().joined(separator: " ")) // connor Sarra john
Sign up to request clarification or add additional context in comments.

5 Comments

no need to pass an empty string joined()
What he ⬆ said.
tried that but receiving this error Referencing instance method 'joined(separator:)' on 'BidirectionalCollection' requires the types 'String.Element' (aka 'Character') and 'String' be equivalent
@Mike ask a new question with that specific problem since this one was closed
print(names.shuffled().joined()) // connorSarrajohn btw not my downvote

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.