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 Solution: Tower Defense Game

Ryan Carlson
Ryan Carlson
3,529 Points

Hi. I was wondering why the sqrt() doesn't work when it's above the "import Foundation"?

The math works out, but I get an error. I'm pretty sure it has something to do with the "import Foundation", I just wanna know what that is. Thanks!

1 Answer

Thomas Dobson
Thomas Dobson
7,511 Points

Any import statements should be above any code that references it. You should generally do your imports at the very top of your file. For example:

//Top of file
import Foundation

//Lots of code
//More code
//You get the idea

sqrt(4)

Generally speaking code is executed top to bottom. If a function or class is called before the referenced material is made available (i.e. Import Foundation) then your call will fail.

sqrt(4) //Because this is executed before its imported, the compiler does not understand how to process it.
import Foundation

I hope this helps!

Ryan Carlson
Ryan Carlson
3,529 Points

Thanks Thomas! Makes total sense!!