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 trialJustus Aberson
1,035 PointsI'm pretty sure, that by the 2nd question, my answer is right, but it keeps saying it isn't correct.
Thanks.
// Enter your code below
let name = "Justus"
let greeting = "\("Hi there,") \(name)"
let finalGreeting = "\(greeting) \("How are you?")"
5 Answers
Soner Güler
16,505 Pointslet name = "Justus"
let greeting = "Hi there, \(name)"
let finalGreeting = greeting +" How are you?"
This code works well
Joe Beltramo
Courses Plus Student 22,191 PointsThe greeting variable will actually cause an error to be thrown, as the compiler will recognize the quotation mark after the Hi there,
statement as the end of the string. When the quote is removed, yes this will work as well. :]
Joe Beltramo
Courses Plus Student 22,191 PointsIn Swift, the use of \(someValue) within a string
is for the purpose of converting some non-string type or some variable into the format of a string without the need of string concatenation (using the +
operator). Therefore, what is being looked for is simply typing the string out and including the variable within the \()
let someString = "This is just a some string and a string interpolation via \(someVariable)"
Joe Beltramo
Courses Plus Student 22,191 PointsI updated my answer to properly display the string interpolation as \()
Justus Aberson
1,035 PointsBut when my code is // Enter your code below let name = "Justus" let greeting = "("Hi there,) (name)" let finalGreeting = "(greeting) ("How are you?")" it still causes an error, what is wrong now? Thanks!
Joe Beltramo
Courses Plus Student 22,191 PointsIt looks like my comment did not include the backslash \ for some reason. My apologies. What I was meaning to say is like my example. The use of the () is meant for variables and non-string types. Therefore when you have a string such as "Hi there", it is just plain text, right? No variables, just words. This is meant to be stored in a variable or printed exactly as a string, without the need of the surrounding parenthesis.
The backslash and parenthesis are used in string interpolation which is when the compiler evaluates the given variable (which may be a string) or non-string type and turns it into a string
.
Example:
let name = "Justus"
let phrase = "Here we will use your name, \(name)"
// phrase would be: "Here we will use your name, Justus"
So what you are looking to do is only put the variable in the () and the rest of the text is just written out.
You are looking to do something like this:
let someName = "Justus"
let someWords = "Hello there, \(someName)." // greeting
let finalOutput = "\(someWords) How are you?" // finalGreeting
Please let me know if this is more clear for you?
Justus Aberson
1,035 PointsThanks Joe!
Justus Aberson
1,035 PointsWhen I use your code it still gives a bump :/
Joe Beltramo
Courses Plus Student 22,191 PointsA bump?
Justus Aberson
1,035 PointsJustus Aberson
1,035 PointsThanks guys.