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 Build a Simple iPhone App with Swift Improving Our User Interface Adding a Pop of Color

Why is view.backgroundColor not accessible outside of certain functions?

I was experimenting with the background color when I quickly discovered that the "view" variable that we used to access the background color is not accessible except within our three functions in the view controller. Even when I try to type 'view.backgroundColor' or even just 'view' in view controller, but not within the code of one of those functions, I get an error.

Why is that? We imported the UIKit at the beginning. Shouldn't that variable be accessible everywhere? How do import what I need to to use it elsewhere?

1 Answer

Andres Oliva
Andres Oliva
7,810 Points

You can't access properties outside the scope of a function. What is written inside the class "ViewController" is known as the interface (property declaration), the functions are the implementation. You may access properties only within the implementation.