Printing on the console
It can be very useful to write output to a log so that you can trace the behavior of some code. As a code base grows in complexity, it can be hard to reason about the order in which things happen and exactly what the data looks like as it flows through the code. Playgrounds can help a lot with this, but this is not always enough.
In Swift, this process is called printing to the console. To do this, you use something called println
, which is short for print line. It is used by writing println
followed by some text surrounded by parenthesis. For example, to print Hello World!
to the console, the code would look like this:
println("Hello World!")
If you put that code into a playground, you will see "Hello World!"
written in the results pane. However, this is not truly the console. To view the console, you can go to View | Assistant Editor | Assistant Editor. A new view will appear to the right of the results pane and it will have a section called Console...