[swift-evolution] Why does URL.checkResourceIsReachable() return a Bool?

Kevin Ballard kevin at sb.org
Mon Aug 8 16:32:41 CDT 2016


The documentation is wrong (for Swift).

For file URLs, this method returns true/false without throwing an error
(at least, if the file doesn't exist there's no error; I don't know if
there are conditions that would cause an error to be returned).

For non-file URLs this method throws an error.

The bit about "For other URL types, false is returned" is written for
Obj-C, where you can completely disregard the error. You can't disregard
the error in Swift, so it doesn't apply (though you can use try? and
treat a nil value the same as false)

-Kevin

On Sun, Aug 7, 2016, at 10:41 AM, Charles Srstka via swift-evolution wrote:
> -[NSURL checkResourceIsReachableAndReturnError:] has taken on several
> forms in Swift during its development, sometimes returning a Boolean
> and an error by reference, sometimes returning Void and using Swift’s
> do/try/catch system. In Swift 3, though, it appears to do both, which
> is weird:
>
> func checkResourceIsReachable() throws -> Bool
>
> All the documentation has to say about this is:
>
> "This method is currently applicable only to URLs for file system
> resources. For other URL types, false is returned.”
>
> It does not, however, say whether non-file URLs are the only thing
> that can cause a return of ‘false’, so I don’t know whether I should
> be checking the return value when checking a file URL or not. This
> results in having to check both the return value *and* the catch block
> each time, which is fairly awkward. I’m also not clear on why checking
> reachability on a non-file URL can’t just return an error like it did
> in the past.
>
> What’s the rationale behind this?
>
> Charles
>
>
>
> _________________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160808/d236b2c2/attachment.html>


More information about the swift-evolution mailing list