Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Types Printing Results

Thomas Wealthy
Thomas Wealthy
1,928 Points

Why does the print syntax append a \n at the end of a line?

When I run the following command in xCode 7.1, it appends \n at the end of the print command (Displayed in the output window)

print("To build an iPhone app we need to Swift")

This shows "To build an iPhone app we need to Swift\n"

Anyone explain why?

5 Answers

Tommy Choe
Tommy Choe
38,156 Points

Hey Thomas, prior to Swift 2.0, there used to be a println function that automatically appended a new line to every argument. However, that function has now been deprecated and the print function has replaced the println function.

Instead of having two different functions that handled separate tasks, print now essentially has "options" for developers to disable the "\n". It has a parameter called "terminator" that can accept an empty string instead of the default \n.

So if you don't want a new line at the end, just do this:

print("To build an iPhone app we need to Swift", terminator:"")

Hope that clarifies things a bit.

Johan Kristensson
Johan Kristensson
883 Points

Hi guys,

My Console Output does not show anything. I am convinced I have it on the correct way as instructed, is there an update that has changed how this work similar to above mentioned problem?

Johan

Tommy Choe
Tommy Choe
38,156 Points

Are you using playground on xcode?

Tommy Choe
Tommy Choe
38,156 Points

Ok. This might sound ridiculous but bare with me since many have suggested this as a solution to your problem. Do you still have the default String "Hello, playground"? If so, can you at least see it displaying in your Console output? If not, try to add that back in and give it a few minutes to display.

Let me know if it still isn't working for you.

I have the exact problem, my console output does not show anything. I tried to uninstall and reinstall xcode, start from scratch, add the original " var str = "Hello, playground" , nothing works

Thomas Wealthy
Thomas Wealthy
1,928 Points

Thank you so much for that! Didn't realise it had changed function! Really appreciate the answer was driving me mad! :o)

Tommy Choe
Tommy Choe
38,156 Points

No problem Thomas. Glad I could help!