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

Andrew Collazo
Andrew Collazo
124 Points

let name = "Andrew" let greeting = "Hi there, \(name)" let finalGreeting = "\(greeting), How are you?" whats wrong?

i keep writing this code

let name = "Andrew" let greeting = "Hi there, (name)" let finalGreeting = "(greeting), How are you?"

and it says im doing it correct on xcode but on teamtreehouse excersise its saying im wrong... please respond to me i never got a response last time.

strings.swift
let name = "Andrew"
let greeting = "Hi there, \(name)"
let finalGreeting = "\(greeting), How are you?"

1 Answer

andren
andren
28,558 Points

The problem is that you use string interpolation (merging variables into strings using \()) instead of string concatenation, which is what the task explicitly asks you to use.

With string concatenation you combine strings and variables by using the + operator. Both of these techniques where covered in the video preceding this challenge, and while they both result in the same string this task is specifically setup to test your knowledge of string concatenation and therefore only accept your code if you use that technique.

So to sum up you have to combine the greeting variable and the " How are you?" string using the + operator, and not use \() at all for the finalGreeting value in order to pass this challenge.

Andrew Collazo
Andrew Collazo
124 Points

can you please show me the correct code and explain how you approached your answer?

The codes that I wrote are

  1. let finalGreeting = "(greeting), How are you?" ///which computes to "Hi there ,Andrew, Andrew"

    1. let finalGreeting = greeting + "," + name///which also computes to "Hi there, Andrew, Andrew"

3.let rGreeting = "(greeting), How are you?"////which Strangely computes to "Hi there, Andrew, How are you?

please help I'm lost.

andren
andren
28,558 Points

The correct code is this:

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

As I stated in my previous answer by using + you can combine two different string together, since the greeting constant contains the string "Hi there, Andrew" in order to get the requested string you can simply add " How are you?" to the end of that string, which is exactly what the + operator does.