Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Matti Helenius
Matti Helenius
421 Points

Stuck on a variable IOS problem.. AGAIN.

Hi!

I'm stuck again, could someone help please.

Heres the question;

Challenge Task 2 of 2

Now that we have an appropriate greeting for our user, let's make a bit more polite by concatenating the greeting string with a second string literal.

Declare a constant named finalGreeting, and concatenate the value of greeting with the string literal " How are you?".

Example: "Hi there, Pasan. How are you?"

Heres the code below;

let hi = "Hi there,"
let name = "Matti"
let greeting = "\(name), How are you?"
let finalGreeting = "\(hi), \(name), \(greeting)"

Thanks in advance.

Katherine Ebel
Katherine Ebel
43,799 Points

Can you post a link to the challenge? The first answer you posted looks correct. Unless they literally want you to have Pasan's name in there...

3 Answers

Katherine Ebel
Katherine Ebel
43,799 Points

Hey Matti, Getting stuck is the best way to learn. :) Your greeting constant should be equal to "Hi there, Matti," Using interpolation with what you have you would get that doing let greeting = "\(hi) \(name)." Using concatenation it would be let greeting = hi + " " + name + "." You want to concatenate the greeting constant with a string literal "How are you?" So your final greeting constant would look like
let finalGreeting = greeting + " How are you?" (Notice the space in front of the H) When you add two strings together you are concatenating them. (That is what the challenge is asking you to do. When you substitute a variable into a string using \() you are interpolating. I am not sure if it makes a difference to pass the challenge...

As you have it now your finalGreeting is Hi there,, Matti, Matii, How are you? Try it out in a playground if you haven't already. Let me know if you are still stuck...

Matti Helenius
Matti Helenius
421 Points

Hi!

I am still stuck :( I tried you're way but it still says "Bummer! Make sure you're using concatenation to create the final string value required!"

I tried it like this

let name = "Matti"

let greeting "Hi there," + " " + name + "."

let finalGreeting = greeting + " How are you?"

and like this

let name = "Matti"

let greeting = "Hi there, \(name)"

let finalGreeting = "\(greeting) + How are you?"
  • Thanks in advance again!
Katherine Ebel
Katherine Ebel
43,799 Points

Ok, so I found the challenge. They want you to use string interpolation for the greeting constant, and string concatenation for the finalGreeting. So you could put

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

I guess they are trying to test that you know the difference between the two (concatenation and interpolation).

Matti Helenius
Matti Helenius
421 Points

Ahh, I tried so much this with (greeting).

You're code seems to be working which is awesome!

Thank you so much. Atleast now ill know be more carefull with the syntax.

Have a awesome weekend!