Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!!