Swift Language Logging in Swift print vs NSLog

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

In swift we can use both print() and NSLog() functions to print something on Xcode console.

But there are lot of differences in print() and NSLog() functions, such as:

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 ProjetName[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() function is very slow compare to print() function.

4 Synchronization: NSLog() handles simultaneous usage from multi-threading environment and prints output without overlapping it. But print() will not handle such cases and jumbles while prating 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.



Got any Swift Language Question?