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 trial

iOS Build a Weather App Dark Sky API Client Interacting With the API

Peter Correa
Peter Correa
17,155 Points

What causes the lazy property 'baseURL' to return an optional URL?

Around 2:27 in the video.

1 Answer

Ian Billings
PLUS
Ian Billings
Courses Plus Student 7,494 Points

Hi Peter,

If you take a look at the Apple Documentation for URL object: https://developer.apple.com/documentation/foundation/url

The reason it returns an optional is because the initialiser for the URL object, that takes a String is a fail-able initialiser. This is because in reality any String can be based to the initialiser, and it may or may not be a valid URL. In the case that it is not valid, it will return nil. E.g. let url = URL(string: "hello world") would return nil, as "hello world" is not a valid URL, though it is a valid String.

Hope that makes sense.

Ian