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 trialMorgan Norelius
682 PointsI don't understand
I don't understand why this is not an interpolated string
// Enter your code below
let name = "Morgan"
let greeting = "\(Hi there,), \(name)"
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Morgan,
The error is in the first part of the string. Remember that with string interpolation, you are asking the compiler to evaluate anything inside \( )
as Swift code, and then render the result as part of the string.
In the latter part of your string you have "... \(name)"
. Thus you are telling Swift, "evaluate the name
variable, and put the result (i.e., the contents of the variable) inside the string". Swift will look at the name
variable, find the value "Morgan" and put that into the string.
Now let's look at the first part of your string, you have "\(Hi there,) ..."
. You are telling Swift to evaluate Hi there,
as Swift code, but since this isn't valid Swift code (it's just part of the string text), Swift is going to complain that there is a syntax error.
When using string interpolation, only put the code you want evaluated inside the \( )
, leave everything else inside the regular string.
Hope that helps.
Cheers,
Alex
Morgan Norelius
682 PointsMorgan Norelius
682 PointsThank you! That makes perfect sense now. βCode evaluatedβ was the explanation that made this clear for me.