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

Using " How are you?" will result in an incorrect answer. Must use "How are you?" without the extra space.

The instructions say we should concatenate the constant 'greeting' with the string literal " How are you?", but this doesn't work. We have to remove the extra space at the beginning of the string to pass the challenge.

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

6 Answers

This would be useful to make sticky or something...if only I could :p

So it's a bug or mistake? Or did I genuinely get it wrong for some reason?

Yeah, I've seen this a few times today

Ah, thanks for the reply Shaun. We'll the answer is there in case people need to see it. I'd imagine it's a bit tricky to get it all working as it should on the back end there :)

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points

first you have to interpolate the constant greeting and them concatenate the string "How are you?" Like this : let finalGreeting = "(greeting)" + "How are you?" i hope this help!

I'm not sure that is correct. I'm pretty sure that would result in the following string: "(greeting)How are you?"

Check out the two lines of code below. They are identical, except the first line has a space before the letter H. Treehouse marks the first one incorrect, but the second one correct.

let finalGreeting = greeting + " How are you?" let finalGreeting = greeting + "How are you?"

My question is, why is the first answer incorrect when we were specifically instructed to add a space before the letter H? Check out the instructions.

I'm only mentioning this in case somebody else gets stuck on this like I did. I guess you can just delete the space for now.

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points

well, i forgot to add the backslash to it. Sorry. let finalGreeting = "(greeting)" + "How are you?"

Ah, I guess it would display correctly if you used a slash (which would make it string interpolation), but the quiz asked us to use concatenation, not string interpolation

The instructions start by saying:


"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."


If you remember, the concatenation example given in a previous video was

let concatenatedAddress = country + province + city

The instructions go on to say:


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


Here, we are clearly instructed to put a space before the letter H in How are you. I believe we are asked to do this so the final string will display "Hi there, Pasan. How are you?", and not "Hi there, Pasan.How are you?"

Putting it all together, I would assume the answer should be:

let finalGreeting = greeting + " How are you?"

Unless I am missing something completely obvious here, I think the above answer is correct, and most users would try it. Worried other users might get frustrated when they find it doesn't work

Again, I ended up getting through the quiz by removing the space, so I'm not sure how your suggestion of using string interpolation applies to my question. For reference, here was the (in)correct answer that got me through the quiz. let finalGreeting = greeting + "How are you?"

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points

Ok, i get it. I was just giving you an idea of how i did it and it worked. Now as far as i know, i don't think they asked for a space before the H in the instruction. In fact they didn't ask for it. So probably that's why your code didn't work. Unles they ask for it, don't do it, follow specifically what the instruction says. Thanks!

Hi there,

Nice to know that string interpolation worked for you. I'll keep it in mind for the future.

Regarding the instructions, here is the exact text copied and pasted from the quiz. It clearly includes a space.

'concatenate the value of greeting with the string literal " How are you?".'

Notice the space.

Josue Gisber
PLUS
Josue Gisber
Courses Plus Student 9,332 Points

Well, guess what? I just try the challenge again, i add an space between the " and the H and it Work!! Screen Shot