Swift: print() vs println() vs NSLog(): Complete Tutorial for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Swift: print() vs println() vs NSLog() — what to choose and why it matters?

\n\n

Everyone who starts learning Swift eventually encounters outputting data to the console. It seems simple, right? But confusion arises: print(), println(), and NSLog() — what's the difference? Why do we need three ways if one would suffice? In this article, we'll break everything down, show examples, and help you never confuse these functions again.

\n\n

The topic is based on a real popular question from Stack Overflow that has gathered thousands of views. We've adapted it for educational content so you can not just memorize, but understand the logic of Swift.

\n\n
\n\n

A brief history: from Objective-C to modern Swift

\n\n

To understand the difference, we need to dive a bit into history. Swift is a young language (released in 2014), but it was created as a successor to Objective-C. Therefore, it peacefully coexists with both "native" Swift functions and older ones from the Apple world.

\n\n
    \n
  • println() — a relic from the very first versions of Swift (before version 2.0). Today it is completely removed.
  • \n
  • print() — the modern replacement for println(), introduced in Swift 2.0.
  • \n
  • NSLog() — a function from the Objective-C world that is still actively used for debugging.
  • \n
\n\n

If you are writing in modern Swift (version 4.x and above), you will no longer encounter println() — the compiler will simply give an error. But it still appears in old tutorials and forums, so it's important to understand what it is and what replaced it.

\n\n
\n\n

print() — the main tool for output

\n\n

How does print() work?

\n\n

Starting with Swift 2.0, print() became the standard way to output data to the console. It is flexible, simple, and fully controllable.

\n\n
let name = "Anna"\nlet age = 25\n\nprint("Hello, \\(name)! You are \\(age) years old.")\n// Output: Hello, Anna! You are 25 years old.
\n\n

The main difference from the old println() is that print() does not add a newline character automatically. Actually, it does, but you can control this with the terminator parameter.

\n\n
// Normal output\nprint("First line")\nprint("Second line")\n// Output:\n// First line\n// Second line\n\n// Output without a newline\nprint("First line", terminator: "")\nprint("Second line", terminator: "")\n// Output: First lineSecond line
\n\n

Parameters of print()

\n\n
    \n
  • items — what you are outputting (can be multiple values separated by commas).
  • \n
  • separator — the separator between items (default is a space).
  • \n
  • terminator — what to put at the end of the line (default is \\n — newline).
  • \n
\n\n
print(1, 2, 3, separator: "-", terminator: "!")\n// Output: 1-2-3!
\n\n

When to use: for regular data output, logging in simple applications, and educational examples.

\n\n
\n\n

println() — a guest from the past

\n\n

If you encounter println() in some old code, know that it is the equivalent of print() from the early versions of Swift. The only difference: println() always added a newline character at the end, and it did not have a terminator parameter.

\n\n
// Old code in Swift 1.x\nlet x = 10\nprintln("Value of x = \\(x)")\n// Output: Value of x = 10 (with a newline)
\n\n

In modern versions of Swift (starting from 2.0), println() no longer exists. If you try to use it, you will get a compilation error. Simply replace it with print() — and everything will work.

Blogs

Book Recommendations