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.
andrew falcone8,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.
Justin HornerTreehouse Guest Teacher
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 Cromer5,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.