<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>Well that's curious. I tested in the REPL for URL(fileURLWithPath: "/foo/bar") and it simply returned false, but running the script does throw an error. And doubly-weird, if I go back to the REPL and prefix the call with `try?` the value becomes nil instead of false. So that's pretty confusing.<br></div>
<div><br></div>
<div>It definitely seems like this method should behave differently than it does today. My suggestion would be to replace it with something like<br></div>
<div><br></div>
<div>func checkResourceIsReachable() -&gt; (Bool, Error?)<br></div>
<div><br></div>
<div>This best approximates the Obj-C behavior where you can just use the return value if you don't care about the reason why it's not reachable, and you can inspect the error if you do care.<br></div>
<div><br></div>
<div>-Kevin Ballard</div>
<div><br></div>
<div>On Mon, Aug 8, 2016, at 03:13 PM, Charles Srstka wrote:<br></div>
<blockquote type="cite"><blockquote type="cite">On Aug 8, 2016, at 4:32 PM, Kevin Ballard &lt;<a href="mailto:kevin@sb.org">kevin@sb.org</a>&gt; wrote:<br></blockquote><div><blockquote type="cite"><div><br></div>
<div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;">The documentation is wrong (for Swift).<br></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;"><br></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;">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).<br></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;"><br></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;">For non-file URLs this method throws an error.<br></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;"><br></div>
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;orphans:auto;text-align:start;text-indent:0px;text-transform:none;white-space:normal;widows:auto;word-spacing:0px;-webkit-text-stroke-width:0px;">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)<br></div>
</div>
</blockquote></div>
<div><br></div>
<div>In my testing, this does not appear to be correct; it actually seems to throw an error in both cases. For a file URL pointing to a file that doesn’t exist, it throws NSFileReadNoSuchFileError, and for an HTTP URL, it throws NSFileNoSuchFileError. This further causes me to question the purpose of having the method return a boolean.<br></div>
<div><br></div>
<div>Here’s the code:<br></div>
<div><br></div>
<div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">import</span><span style="font-variant-ligatures:no-common-ligatures;"> Foundation</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><span style="font-variant-ligatures:no-common-ligatures;"></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">let</span><span style="font-variant-ligatures:no-common-ligatures;"> fileURL = </span><span class="colour" style="color:rgb(112, 61, 170)">URL</span><span style="font-variant-ligatures:no-common-ligatures;">(fileURLWithPath: </span><span class="colour" style="color:rgb(209, 47, 27)">"/asdfasdfasdf"</span><span style="font-variant-ligatures:no-common-ligatures;">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(187, 44, 162)">let</span><span class="colour" style="color:rgb(0, 0, 0)"> httpURL = </span><span class="colour" style="color:rgb(112, 61, 170)">URL</span><span class="colour" style="color:rgb(0, 0, 0)">(string: </span><span style="font-variant-ligatures:no-common-ligatures;">"<a href="http://asdfasdfasdf.com/asdfasdfasdf">http://asdfasdfasdf.com/asdfasdfasdf</a>"</span><span class="colour" style="color:rgb(0, 0, 0)">)!</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><span style="font-variant-ligatures:no-common-ligatures;"></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">do</span><span style="font-variant-ligatures:no-common-ligatures;"> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61, 29, 129);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(187, 44, 162)">if</span><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(187, 44, 162)">try</span><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(79, 129, 135)">fileURL</span><span class="colour" style="color:rgb(0, 0, 0)">.</span><span style="font-variant-ligatures:no-common-ligatures;">checkResourceIsReachable</span><span class="colour" style="color:rgb(0, 0, 0)">() {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(61, 29, 129)">print</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">"file URL is reachable"</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;">&nbsp; &nbsp; } </span><span class="colour" style="color:rgb(187, 44, 162)">else</span><span style="font-variant-ligatures:no-common-ligatures;"> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(61, 29, 129)">print</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">"file URL returned false"</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;">&nbsp; &nbsp; }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);"><span class="colour" style="color:rgb(0, 0, 0)">} </span><span style="font-variant-ligatures:no-common-ligatures;">catch</span><span class="colour" style="color:rgb(0, 0, 0)"> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(61, 29, 129)">print</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">"file URL threw error: </span><span class="colour" style="color:rgb(0, 0, 0)">\</span><span style="font-variant-ligatures:no-common-ligatures;">(</span><span class="colour" style="color:rgb(0, 0, 0)">error</span><span style="font-variant-ligatures:no-common-ligatures;">)"</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;">}</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><span style="font-variant-ligatures:no-common-ligatures;"></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">do</span><span style="font-variant-ligatures:no-common-ligatures;"> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61, 29, 129);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(187, 44, 162)">if</span><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(187, 44, 162)">try</span><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(79, 129, 135)">httpURL</span><span class="colour" style="color:rgb(0, 0, 0)">.</span><span style="font-variant-ligatures:no-common-ligatures;">checkResourceIsReachable</span><span class="colour" style="color:rgb(0, 0, 0)">() {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(61, 29, 129)">print</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">"http URL is reachable"</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;">&nbsp; &nbsp; } </span><span class="colour" style="color:rgb(187, 44, 162)">else</span><span style="font-variant-ligatures:no-common-ligatures;"> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(61, 29, 129)">print</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">"http URL returned false"</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;">&nbsp; &nbsp; }</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);"><span class="colour" style="color:rgb(0, 0, 0)">} </span><span style="font-variant-ligatures:no-common-ligatures;">catch</span><span class="colour" style="color:rgb(0, 0, 0)"> {</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209, 47, 27);"><span class="colour" style="color:rgb(0, 0, 0)"> </span><span class="colour" style="color:rgb(61, 29, 129)">print</span><span class="colour" style="color:rgb(0, 0, 0)">(</span><span style="font-variant-ligatures:no-common-ligatures;">"http URL threw error: </span><span class="colour" style="color:rgb(0, 0, 0)">\</span><span style="font-variant-ligatures:no-common-ligatures;">(</span><span class="colour" style="color:rgb(0, 0, 0)">error</span><span style="font-variant-ligatures:no-common-ligatures;">)"</span><span class="colour" style="color:rgb(0, 0, 0)">)</span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;">}</span><br></div>
</div>
<div><span style="font-variant-ligatures:no-common-ligatures;"></span><br></div>
<div><br></div>
<div>And the output:<br></div>
<div><br></div>
<div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;"><b>file URL threw error: Error Domain=NSCocoaErrorDomain Code=260 "The file “asdfasdfasdf” couldn’t be opened because there is no such file." UserInfo={NSURL=<a href="file:///asdfasdfasdf">file:///asdfasdfasdf</a>, NSFilePath=/asdfasdfasdf, NSUnderlyingError=0x100903eb0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}</b></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;"><b>http URL threw error: Error Domain=NSCocoaErrorDomain Code=4 "The file doesn’t exist."</b></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;"><b>Program ended with exit code: 0</b></span><br></div>
</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;"><b></b></span><br></div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span style="font-variant-ligatures:no-common-ligatures;"><span class="font" style="font-family:Helvetica"><span class="size" style="font-size:12px">Charles</span></span></span><br></div>
</blockquote><div><br></div>
</body>
</html>