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

*sigh*-- Works in Xcode, not in challenge.

Attached code is designed to take the phrase "Hi there" and add it to my name as an interpolated string. I couldn't figure out why the challenge editor wouldn't take my code so I wrote it in Xcode to get the error messages.

Hey whaddyaknow! Xcode thinks it is fine and it returns the desired result: Hi there, Erick.

How can I get this correct code past the challenge editor?

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

3 Answers

Zachary Kaufman
Zachary Kaufman
1,463 Points

Don't have ("") around the Hi there I think that will let it pass. I hope this helps

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

I have to say again... I'm very surprised this actually works in Xcode. (And if someone could explain to me why it does compile in Xcode...)

You are on the right track, but you cannot interpolate a String. Only variables and constants can be interpolated. Therefore, you can't have parentheses around the string portion... only the variable.

Below is the corrected code for your review. I hope it makes sense.

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

Keep Coding!

:dizzy:

Zachary Kaufman
Zachary Kaufman
1,463 Points

I think it works in xCode because it is correct technically. You can input anything you want with () including strings. But he said in his video at some point that it doesn't look near as clean if you do a ton of () and you should try to only use () to call in variable/constant names. So I think he was trying to get us in the habit of using () only on variable and constant names and how to properly do that. Hence why the grader counted it wrong but xcode said it was fine. (This is all just speculation)

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Well, that makes sense. I never tried to interpolate an actual string, so I just assumed that it was syntactically incorrect. Thank you Zachary for clarifying it for me. I've seen this question so many times and was always confused by why Xcode was compiling it. I was always under the impression that only a variable/constant could be compiled. :smile:

Zachary Kaufman
Zachary Kaufman
1,463 Points

No problem I guess it's just one of the things that stuck with me. Although now that I am in OOP Swift, nothing is sticking haha I need to rewatch all the videos in Struct. Oh well, its a learning experience

Thank you everyone. It might be nice that Xcode 'figured it out' and went and compiled it... but that kind of leeway makes it confusing for newbies like me.