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 Build a Weather App Dark Sky API Client From JSON to CurrentWeather

andrew falcone
andrew falcone
8,651 Points

Are typealiases only useable in the file that they are declared?

At 5'39" Pasan says we could use JSON as a typealias, but I'm getting an error "use of undeclared type 'JSON'". The weather.swift file seems to be unaware of the typealias declared in the JSONDownloader.swift file.

andrew falcone
andrew falcone
8,651 Points

I moved the typealias for JSON outside of the JSONDownloader class (same file, but before the class is defined) and the error went away. It seems like typealiases are global unless they are defined within a class/struct?

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Andrew,

I believe type aliases in Swift have the same scoping rules as variables. If you want the type alias to be available globally, define it outside of the class, otherwise, define it in a class to keep the scope at the class level.

I hope this helps. Happy coding 🙂

Cory Cromer
Cory Cromer
5,171 Points

I'll also add that you can access the the type alias from another class and use it through dot notation. So, using the example in the video, you could access the type alias in JSONdownloader in the Weather extension by using JSONdownloader.JSON.