0

I have a simple Swift program running in a macOS playground in Xcode. This displays its output from print statements in the debug area on the Mac screen. Is there some simple method with which I can redirect this output into a file?

espri
  • 13
  • 1
  • 4
  • 1
    Does this answer your question? [Append text or data to text file in Swift](https://stackoverflow.com/questions/27327067/append-text-or-data-to-text-file-in-swift) – Joakim Danielson Aug 21 '20 at 12:30
  • Writing to a file is trivial. The question is exactly what you mean by “redirect this output”. Are you willing to change the print statements? Or were you hoping that `print` would magically change its meaning? – matt Aug 21 '20 at 13:06
  • @matt, I had hoped that print had a parameter like to: which would allow you to write to an open file but it doesn't as far as I know. What was in my mind was that at the Unix level redirecting stdout is easy without changing the program and I hoped there was some way to access something like that from Swift. Thanks for your help. – espri Aug 21 '20 at 19:00
  • 1
    It does! If you're willing to change the print statements, you can add a `to:` parameter. You need an output stream that appends to a file. – matt Aug 21 '20 at 19:11
  • 1
    Okay, so https://nshipster.com/textoutputstream/ – matt Aug 21 '20 at 19:18
  • @Joakim Danielson, thanks. The suggestion seems quite complicated and I must admit not having understood it all. However, the workaround suggested by Sweeper did produce the result I wanted, though it meant using the CLI rather than doing everything in the playground. So I have gone that way for now. – espri Aug 21 '20 at 19:58
  • @matt, wow! That seems to do just what I want. I hadn't found the version of print with the to: parameter. However, I'm going to have to read your blog a few more times before the stream idea really sinks into my head and I can try it in my playground. It does though confirm my trust that Swift would support something like this. Thanks again. – espri Aug 21 '20 at 20:13
  • Not my blog, it's a different Matt! :) – matt Aug 21 '20 at 22:24

1 Answers1

1

If you don't want to change the code, you can run the Contents.swift file in the playground using the swift command in Terminal.app, and use > to direct the output to a file, e.g.:

swift YourPlayground.playground/Contents.swift > output.txt

This will cause the output to be written to output.txt in the working directory.

If you can change the print statements, you can pass your own implementation of TextOutputStream (which writes to a file) to the to: parameter of print. For more info, see this link.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Many thanks for the tip. That did allow me to create a file as the program's output. It is a fine workaround (and Contents.swift is also useful for saving the playground code as a file), though I had hoped to be able to write the file directly from the playground. – espri Aug 21 '20 at 19:48
  • @espri It appears that I completely misunderstood your question! I thought you didn't want to change the code! Oh well, it seems like matt's link has helped you anyway. – Sweeper Aug 22 '20 at 00:47
  • no problem. I found your suggestion useful and was also happy to learn about Contents.swift. Where does that come from? – espri Aug 22 '20 at 10:26
  • @espri The `.playground` "file" that Xcode creates is not actually a file. It's actually a directory containing (among other things) a `Contents.swift` file. And when you edit the playground's source code, you are actually editing `Contents.swift`. – Sweeper Aug 22 '20 at 10:29