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 2.0 Basics Swift Types String Manipulation

Interpolation let name = "Machok" let greeting = "Hi there" let interpolatedGreeting = "\(greeting), \(name)."

Challenge on interpolation! This code won't run in the editor. It says error but I can't seem to see where I went wrong.

let name = "Machok" let greeting = "Hi there" let interpolatedGreeting = "(greeting), (name)."

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

Consider the code below:

let name = "Machok"
let greeting = "Hi there, \(name)."
let finalGreeting = greeting + " How are you?"

What they are showing you here with the greeting string is that you can interpolate a value (the name value in this case) inline using a backslash and surrounding the value name with brackets. Take note of the comma and space before the value to ensure it spaces right, and the full stop after the value to get the punctuation correct.

The second step with the finalGreeting is showing you that you can concatenate strings together too. You can see we have the greeting value (from the previous step) followed by a string literal. Take note of the space at the start of the string literal. This is because a space would not be added in concatenation and you need to take care to add them yourself where needed.

thanks a lot that solved my error