Swift: print() vs println() vs NSLog()

Cover Image for Swift: print() vs println() vs NSLog()
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Swift: print() vs println() vs NSLog()

So you're new to Swift and wondering what's the deal with these three printing functions: print(), println(), and NSLog().💭

Let's break it down!🔍

print()

The print() function is a built-in function in Swift that prints the specified values you provide. It is similar to the print() function in other programming languages like Python, JavaScript, and Ruby.👍

Here's an example:

let message = "Hello, World!"
print(message)

This will simply print "Hello, World!" in the console.

Now, what about the other two functions?🤔

println()

The println() function was available in earlier versions of Swift but has been deprecated since Swift 2.⚠️

In Swift 2 and later versions, you should use print() instead. So forget about println()!🙅‍♂️

NSLog()

NSLog() is not actually a Swift function, but rather a function from the Objective-C runtime library. It is used to output log messages that can be helpful for debugging purposes.🐞

Here's an example:

let message = "Debugging info"
NSLog("%@", message)

This will output the message "Debugging info" along with additional information like the date, time, and process ID.

So in summary, use print() for general-purpose printing, and NSLog() for more advanced debugging or logging purposes.

Now that you know the differences, you can confidently choose the appropriate printing function in Swift.🎉

Conclusion

Printing values in your Swift code is essential for debugging and understanding your program's behavior. By utilizing the print() function for general-purpose printing and NSLog() for advanced debugging, you'll have the tools you need to navigate any printing scenario.💪

Remember, println() is a thing of the past, so always stick with print() in your Swift code.

Now go forth and print away!🚀

Have any questions or other Swift topics you'd like me to cover? Let me know in the comments below!📝

References


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello