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

Ayush Behere
Ayush Behere
566 Points

Cannot compile my response

Hi,

I'm having a problem with getting the answer to this challenge. I type my response in xcode and it works perfectly, so a bit confused about why it's not working here. Thanks in advance!

let name = "Ayush" let greeting = "("Hi there"), (name)."

strings.swift
// Enter your code below

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

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Ayush Behere,

You only need to use String Interpolation to convert the values stored inside of a variable/constant into a String. The string, "Hi there, " is already a string literal. Therefore, you don't need to use interpolation here.

See my code below:

// Enter your code below

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

Good Luck!

Ayush Behere
Ayush Behere
566 Points

Thanks for your response Steven! I guess I think I made that mistake because in the video, when combining the street number with the street name, interpolation was used for the street number. Is this because that was a number and not a string, whereas "Hi there," is already a string?

Steven Deutsch
Steven Deutsch
21,046 Points

Yes, if you had some variable, number lets call it, that is storing an integer value of 4 - you would use String Interpolation to extract that value from the number variable and write it as a string. However, if you wanted to join that number with what is known as a String literal, you wouldn't have to use interpolation on the String literal, because it is already a String.

let number: Int = 4
// Here we don't need to use interpolation on "The number of houses is"
// This is because it is already a string!
// However, our number variable is of type Int
// We want to take that value out of the variable and replace it inside of our string
// So we use interpolation for this
let message = "The number of houses is \(number)"

Keep in mind that we would do the same thing to extract the value of number if it was a type other than Int, let's say it was a String value as well. You use String interpolation to extract the value from the variable or constant. If you are writing the value out, literally, you don't need to use interpolation.

Good Luck

Ayush Behere
Ayush Behere
566 Points

That helps a lot, thanks!