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

I think something wrong. I think it is always right

Why is it error ? please explain for me. Thanks

strings.swift
// Enter your code below
let name = "Linda"
let greeting = "Hi there," + "\(name)" + "."

3 Answers

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

I'm guessing you've done programming in other languages that uses the + symbol to concatenate strings together. This is not how it works in Swift. Try this code for the greeting constant instead:

let name = "Linda"
let greeting = "Hi there, \(name)."
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hey Jennifer Nordell

Swift does actually use the + symbol for concatenation, like most other languages. What you are doing here is "String Interpolation", which is another way to join up strings in Swift. Both have pros and cons depending on what you are trying to accomplish, but both are valid ways in Swift. :smiley:

:dizzy:

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

As Jason has pointed out, and I was unclear about Swift does also use concatenation. But they specifically asked for interpolation which is different. Sorry to muddy the waters here :)

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

All good Jennifer.

There are times I muddle up the syntax of different languages. :)

Thank you for your participation in the Community Forums. :thumbsup:

:dizzy:

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey William,

Jennifer is correct for this challenge. But this challenge is not about concatenation. It is about using `String Interpolation". You have a little of both in you answer.

This string would be close to what you have, if the challenge was asking for concatenation.

let greeting = "Hi there, " + name + "." //Concatenated String in Swift

Please refer to Jennifer's response for the correct interpolated string.

Hope this helps. Keep Coding! :dizzy:

Thanks all !!