0

I need to write a dictionary to a text file. I couldn't find anything handy. But writing an array is solved only for strings:

let pieces = [ "1", "5", "1", "4" ]
let joined = "\n".join(pieces)

How could I join integers without creating my own loop?

If there is a simple solution for dictionary itself, that would be better! ;-)

Peter71
  • 2,180
  • 4
  • 20
  • 33

1 Answers1

1

So you have an array of Int

let numbers = [1,2,3,4,5]

And you want to concatenate the text representations of these numbers without writing a loop right?

You can write this:

let joined = "\n".join( numbers.map { "\($0)" } )

Now joined has this value: "1\n2\n3\n4\n5"

Hope this helps.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148