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

Can someone please help me with this?

So I did some experimenting and came across something interesting... Take a look!

So when I write this code, i get an error!

var mySentence = "I like to eat (myConstant)" let myConstant = "oranges"

However, when I write myConstant above mySentence, the error goes away.. why? Can someone please explain this to me in detail...?

1 Answer

Matthias Vanspringel
Matthias Vanspringel
2,529 Points

When you are creating your sentence you call the value of the myConstant constant, but the system doesn't know the value of that constant, because that doesn't exist. So the system will try to make a sentence, but does not know what to write. Make sure to declare your constant or variable before using it. Also don't forget to use the backslash when using string interpolation, something like: (myConstant).

A program will always run top-down, each line will be executed line after line, starting from the top. That is the reason why you should always first declare you variable or constant.

Happy coding!