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 trialEli MBS
1,158 Pointsswift 2.0 interpolated string
Hey Guys! Can anyone help me with this. I don't get the task completely..
// Enter your code below
let name = "Elias"
let greeting = ("Hi there, \(name)")
1 Answer
Anjali Pasupathy
28,883 PointsHello, Elias! Your code is correct. You don't necessarily need to encapsulate your greeting String in parentheses, but it doesn't harm your code to do so. However, the coding challenge doesn't accept an answer where greeting is inside parentheses. Other than that, all you're missing is the period at the end of greeting:
let greeting = "Hi there, \(name)."
Hope this helps!
EDIT: I just played around with greeting in a playground, and came to the realization that encapsulating greeting in parentheses does make a difference. By putting greeting in parentheses, you're creating a constant of type (String) (a tuple with a single String value inside of it), rather than just a String. This makes a difference because a tuple is very different from a String. A tuple is an object that acts like a container for one or more objects of any type - for example, if you were to write the following code:
let name = "Me"
let greeting = "Hello there, \(name)!"
let nameAndGreeting = (name, greeting)
the type for nameAndGreeting would be (String, String).
To access and manipulate the String inside the tuple, you have to access the index of that String (for example, nameAndGreeting.0 would return name, and nameAndGreeting.1 would return greeting). In the case of your code, if you want to do anything with the String inside of greeting, you'd have to access greeting.0 rather than doing anything directly to greeting.
If this doesn't make sense, don't worry - Pasan covers tuples very well later on. Just know that putting your greeting String in parentheses makes an object of type (String), which behaves very differently from an object of type String.
Eli MBS
1,158 PointsEli MBS
1,158 PointsOh just saw it. Thank you very much! :)
Anjali Pasupathy
28,883 PointsAnjali Pasupathy
28,883 PointsYou're welcome! (:
Anjali Pasupathy
28,883 PointsAnjali Pasupathy
28,883 PointsJust so you know, I updated my answer to make it more accurate - putting the String you assign to greeting in parentheses does make a difference.