Q:

Replace printed out text

I created a command line tool which does some long actions, and I want to notify the user through print statements, like:
"10% Done"
"20% Done"

on what percentage of the operation is done. If I use println, each progress will be printed into a new line (obviously), but is there a way in swift, that I can replace the last printed text with a new one, so on the console you can read only the actual percentage, not the actual and the ones that printed before?

A:

If the output goes to the Terminal then you can use the fact that
\r (Carriage Return) moves the "cursor"  to the start of the current
line, without advancing to the next line:
print("10% done \r")
print("20% done \r")
print("100% done\n")

(But note that this does not work in the Xcode debugger console.)

A:

Something like this works on the terminal console:
import Foundation
for i in 1...10 {
    print("\(i) \r", terminator: "")
    fflush(stdout)
    sleep(1)
}

