Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

This quick swift article will explain to you what the difference is for print(), println() and NSlog().

As per the newest version of Swift, println() no longer exists. RIP dude. Apple Docs

I got a quick email asking me to clarify this, so here goes.

For those of you who have used these commands in your Swift code or just playing around in Xcode, You will see that aesthetically their outputs are rather the same. There are however a few differences as to what these commands actually do:

println() vs print()

Prior to the release of Swift 2, println() would just add a newline at the end of the results it produces, unlike print() which did not. This was about the only difference.

However, since the release of Swift 2, println() is no longer used. One would generally use print (either without appendNewline parameter, or having that parameter set to true) to print a line followed with a newline character. You would use print() with appendNewline of false if you want to print a string without a newline at the end.

Starting in Swift 2.0, the recommended method of printing without newline is:

print("Hello", terminator:"")

As you can see the print() function has changed completely since the late revision of Swift 2, now it looks much simpler and there are variant of method to print to standard console.

The method signature for print looks something like this,

public func print(items: Any..., 
                                         separator: String = default, 
                                        terminator: String = default, 
                             inout toStream output: Target)

As you can see, all the other parameters except items takes the default parameter. Note: You can pass them while calling but they are not mandatory.

What does the basic version of print output:

print("Swift 2 is awesome.")
// This is will ouput:
// Swift is awesome.

Using the terminator in print()

Now we are going to use terminator to add a . to the print.As you can see when using terminator, it appends the new prints on the same line:

print("Swift", "is", "awesome", separator:" ", terminator:".")
// This will output:

// Swift is awesome.

Using separator in print()

Using the separator to concatenate multiple items and print them to console is achieved like so:

print("Swift 2", "is", "awesome", separator:" ")
// This will output:

// Swift 2 is awesome

Concatenating multiple print() functions

Let us assume that the two prints as follows:

print("The fish", terminator: " ")
print("Tomorrows World")
//This will give us the output like so: 

// The fish Tomorrows World

From the output above you can see that they make no sense together, when using terminator, you should be careful that the contents are relevant to the content that will be on the same line.

Print() with Custom Object

Consider the following class: Person, struct Address { let city: String }

class Person {
    var name: String = "Jordan"

    var addresses:[Address] = [
        Address(city: "Tampa"),
        Address(city: "Miami")

This means that you can have your custom string printed when you print an instance of class Person. For this, you can conform to CustomStringConvertible Protocol. Take a look at the apple documention for more information on CustomStringConvertible Protocol.

NSLog() vs print() / println():

Well, I almost forgot to mention about NSLog() for a moment. I’ll be quick with this:

Firstly, you will notice that NSLog() is a slower process. Also NSLog() will add a timestamp and an identifier to its output. This will not happen from println(). A great thing is that NSLog() will synchronize it’s log statements which is great if you are issuing a few different logs from a few different threads concurrently. This means that they will not overlap each other like the would with println(), which can mess up your simultaneous actions from separate threads. When you are testing your application on an actual device you will notice that NSLog() statements appear in the device’s console whereas println() only appears in the debugger

I hope that helps clear some things up.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

I work in the shadows. You can thank me by being a good soul.