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,072 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.