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 Network Programming with Swift 2 Modeling Forecast Data Modeling the Current Weather

Harish Yerra
Harish Yerra
10,031 Points

Why aren't we using optionals?

It says in the forecast api docs that the information may be present. As a result, shouldn't we be using optionals since the values could be nil?

1 Answer

Anthony Boutinov
Anthony Boutinov
12,695 Points

The documentation states (and the teacher points it out in the video at about 0:04:20) that some properties are guarantied to be present for currentWeather request, other properties are guarantied to be present at daily forecast, and so on. But overall, when we receive data for any query, we must make sure that each property that wet are interested in is present.

And if we run into a situation where for a given request we got nil values, there is no point in creating CurrentWeather object. Instead, an error must be thrown (and handled, for example by trying to get the data again).