print vs NSLog

suggest change

Swift code can use both print() and NSLog() to write to the Xcode console.

They differ in several ways:

1 TimeStamp: NSLog() will print timestamp along with the string we passed to it, but print() will not print timestamp.

e.g.

let array = [1, 2, 3, 4, 5]
print(array)
NSLog(array.description)

Output:

[1, 2, 3, 4, 5]

2017-05-31 13:14:38.582 ProjectName[2286:7473287] [1, 2, 3, 4, 5]

It’ll also print ProjectName along with timestamp.

2 Only String: NSLog() only takes String as an input, but print() can print any type of input passed to it.

e.g.

let array = [1, 2, 3, 4, 5]
print(array) //prints [1, 2, 3, 4, 5]
NSLog(array) //error: Cannot convert value of type [Int] to expected argument type 'String'

3 Performance: NSLog() is much slower than print().

4 Synchronization: NSLog() serializes output from multiple threads so lines do not overlap. print() does not, so concurrent calls can jumble the output.

5 Device Console: NSLog() outputs on device console also, we can see this output by connecting our device to Xcode. print() will not print output to device’s console.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents