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

show answers in challenges to find out exactly what went wrong been on this challenge for a while, +went back to video

cannot figure out.

strings.swift
// Enter your code below
let name = "marco"
let greeting = "\(hi there)" "\(name)"

In this task we're going to declare two strings. First, declare a constant named name and assign to it a String containing your name.Second, declare a constant named greeting. Set the value of greeting to an interpolated string that combines "Hi there, " with the string stored in the name constant.As an example, the final value of greeting could be "Hi there, Linda

2 Answers

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

Hi Marco! I can see that you've worked on this. We use the backslash and parentheses when we want to insert the value of a variable into that spot inside a string literal. A string literal is simply a set of characters inside quotation marks. In your code, "marco" is a string literal.

Also, challenges are very strict and any string they get back must match to the letter. This includes capitalization and punctuation. Your "hi there" should actually be "Hi there, " with a comma and space. Take a look:

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

This will assign the string "Hi there, marco" to the constant greeting. If we had left off the backslash and parentheses from the name variable and had this "Hi there, name" and then tried to print out greeting it would print out "Hi there, name" instead of "Hi there, marco".

Hope this helps clarify things! :sparkles:

You were really close, and this is a bit complicated compared to how other programming languages handle it, so don't feel bad.

  • "Hi there " is a literal string so does not need the (). Just leave it in quotes.
  • Name was perfectly done, except that you used separate "" around it. It should just be one large "".
  • You can read Apple's explanation of Strings here.
// Enter your code below
let name = "Marco"
let greeting = "Hi there, \(name)"

I recommend you rewatch the video at 10:19. If you have questions on the second step, post below.