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

Martin Perez
Martin Perez
192 Points

String Interpolation

I don't understand why it is not able to compile my string in the code challenge, according to the instructions I am to declare only 2 constants. The first constant being named "name" with a value of my name (Angel). The second constant being an interpolated string named "greeting" with a value of "Hi There, Angel". The way I have written the code seems to be the logical way to me to create the constant "greeting" have the value assigned to it of "Hi There, Angel" without first assigning the value of "Hi There, " to the name "greeting". If I do it that way then it won't let me change the value of "greeting" to an interpolated string.

Help please.

strings.swift
// Enter your code below

let name = "Angel"
let greeting = "\("Hi There, ") \(name)"

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Angel,

You have a few issues with your code, only one of which is causing your code to technically fail the challenge.

The first issue is that you are not being asked to evaluate "Hi There ," as an expression, but that is what you are doing by putting the string literal inside the \(). The syntax \() is used exclusively to evaluate some code inside a string, if you just want to have text inside a string, you just put it inside quotes.

The second issue is that your string literal is wrong. The challenge tells you to use the text "Hi there, " but you have the text "Hi There, ". On this occasion, the challenge checker is passing you despite having a different string. You should not expect to be so lucky in future challenges.

The next issue (and the one that is actually causing you to fail the challenge) is that you have two consecutive spaces in your string and the challenge only wants one. Your first space is in the string you are evaluating ("Hi There, ") and then right after you close the \() you add another space. When the compiler reads the following:

"\("Hi There, ") \(name)"

It converts \("Hi There, ") to Hi There, and \(name) to Angel inside your string, so greeting ends up being:

"Hi There,  Angel"

Hope that clears everything up for you,

Cheers

Alex

Martin Perez
Martin Perez
192 Points

Thanks Alex for clearing that up for me! I appreciate that.