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

Aditya Gawade
Aditya Gawade
1,182 Points

string interpolation

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

ps the first two code lines are answers part 1 questions

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

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You were on the right track. This challenge is specifically testing you to see if you can get similar results using different methods. In the greeting constant you're using string interpolation. String interpolation is the use of the backslash and parentheses to insert the value of a variable in that spot. And you executed that part flawlessly!

However, take another look at the instructions for the last part: Declare a constant named "finalGreeting", and concatenate the value of "greeting" with the string literal "how are you".

Here they want you to use concatenation. They just want to know that you can do it both ways. Here's an example of concatenation.

let phrase1 = "Hi there, "
let phrase2 = "Aditya!"
let totalPhrase = phrase1 + phrase2

The result of the concatenation (the use of a plus sign to put together strings) is that totalPhrase will contain the string "Hi there, Aditya!". Give it another shot with these hints in mind! :sparkles:

Aditya Gawade
Aditya Gawade
1,182 Points

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

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

these were the two codes i tried and both were wrong

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Aditya Gawade the second one would have been correct if it had had a backslash and your quotes had been placed properly in the finalGreeting:

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

If you put the variable name inside quotes you must also have the backslash and parentheses, so that it knows to insert the value of the variable there. If it's outside the quotes you can just used the variable name.

In your attempts, if you'd printed finalGreeting somewhere it would have literally printed "(greeting) + How are you?". In the second attempt it would have literally printed "greeting + How are you?". Hope this makes sense! :sparkles: