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

Pedro Fernández
Pedro Fernández
561 Points

Code that works in xCode is not working in the treehouse editor

Hi I wrote the following code to complete the challenge but the web editor does not seem to accept it as the correct answer and I do not quite seem to be able to figure out the issue with the code since it works perfectly fine in a xCode playground.

thank you

strings.swift
// Enter your code below
let name = "Pedro"
let greeting = "\("Hi there, ")\(name)"
let finalGreeting = "\(greeting)\. (" How are you?")"

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! First, I'm not sure why your code is working in your version of Xcode because it will not compile in mine. It gives syntax errors on the third line due to an invalid escape sequence.

Secondly, when we use string interpolation, we are retrieving the string value of a variable and putting it in that spot. There is no need to put a string literal inside the backslash and parentheses although this, by itself, will not cause an error.

Thirdly, the challenge asks that for the last step you use concatenation. Concatenation uses a plus sign + to add two strings together. This is how I reworked your code:

// Enter your code below

let name = "Pedro"  //assign "Pedro" to name
let greeting = "Hi there, \(name)"  //assign "Hi there, Pedro" to greeting
let finalGreeting = greeting + " How are you?"  // concatenate greeting with " How are you?"

Remember that when we use the backslash and parentheses we are telling Swift to insert the value of the variable at this spot inside our string literal. If we were to now use print(finalGreeting) the result would be "Hi there, Pedro How are you?"

Hope this helps! :sparkles: