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 trialandrew falcone
8,651 PointsAre 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.
2 Answers
Justin Horner
Treehouse Guest TeacherHello 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
5,171 PointsI'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.
andrew falcone
8,651 Pointsandrew falcone
8,651 PointsI 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?