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

Adam Al-Najjar
Adam Al-Najjar
1,662 Points

Confused about compiling issues in the 'FizzBuzz' challenge

Hello! Basically my issue is this, whenever I try to compile my results on this challenge, it wont work. When I look over the code and compare it to what the fizzbuzz answer video does, it's the same thing. What I noticed was that it seems the

return "(n)" }

part of the code is not compiling properly. Even when untouched, the final } is quoted out even though there is not an empty open quote anywhere in the code.

I understand that when resolving this challenge, you need to return "String" often, however the word 'return' isn't acting like it's in a string, nor is the "(n)", however everything after it is acting like it's in a string. So my final } isn't compiling.

I hope I'm making sense, is there any way I can fix this issue. Maybe it is a coding issue and I'm willing to learn! Thank you!

1 Answer

Hey Adam, without seeing all of your code it's hard to be certain this is the only issue but I think you're on the right track.

The problem with your return statement is that you're missing a backslash for the sting interpolation. Basically, you want to tell the compiler that you want to contents between \( and ) to be treated as code instead of as a string literal. In this case, we don't want to the compiler to literally read out the literal string as \(n), instead we want to print the value of the n variable in the string.

This might be a little easier to understand if we look at the reverse situation and talk about escape characters.

Backslash is an escape character in Swift, basically it lets you tell the compiler you're about to use a symbol that normally tells Swift to do something but you want it to treat that character as a string literal.

For example, let's say you want to assign the following string to a variable:

I asked Adam "how are you doing"?

In Swift we might try to do it like this:

let myGreeting = "I asked Adam "how are you doing"?"

This will throw an error because the compiler is looking at your string and thinks it ends early, then it gets some commands in doesn't recognize and a consecutive string it doesn't know what to do with.

"I asked Adam "  how are you doing  "?"
↑∙∙∙∙∙∙∙∙∙∙∙∙∙↑  ↑∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙↑  ↑∙↑
|             |  |               |  | |_ 2nd String Ends
|             |  |               |  |_ 2nd String Begins
|             |  \               /
|             |   Invalid commands
|             |
|             |_ String Ends
|_ String Begins

So instead, we use an escape character to tell the compiler, this next character usually means something to you but just treat it literally as part of the string:

let myGreeting = "I asked Adam \"how are you doing\"?"

When we use string interpolation we're doing the reverse of using an escape character but it's the same basic concept, the compiler needs to know what is code and what is a literal string.

return " \( n) " }
↑∙∙∙∙∙ ↑ ↑  ↑  ↑
|      | |  |  |_ Okay, we're done with code, any remaining characters before " are string literals
|      | |  |_ n is a variable so we're going to get the value of n
|      | |_ \( Means we're going to evaluate whatever follows as code until we reach )
|      |_ String begins
|_ Our return command

Since the compiler doesn't see a backslash in your return statement it doesn't know that it needs to return the value of n as a string and because the TreeHouse test is looking for n you get a compiler error.