Mischa
Mischa

Reputation: 17269

Is it safe to assume that the initializer URLComponents(url:resolvingAgainstBaseURL:) never returns nil?

When you have a URL in Swift and want to create a URLComponents instance from it, you have to use this initializer:

init?(url: URL, resolvingAgainstBaseURL: Bool)

I never understood why this initializer is failable. The initializer URL(string:) is already failable and returns nil if the string does not represent a valid URL. Therefore, I would assume that a URL (once successfully created) is always valid. Consequently, it should be possible to always construct a URLComponents instance from any given URL.

However, if this assumption was true, the above initializer wouldn't need to be failable. Why is it and under which circumstances is it guaranteed to succeed and return a non-nil value?

Upvotes: 1

Views: 29

Answers (0)

Related Questions