NSURL 路径与绝对字符串 [英] NSURL path vs absoluteString

查看:100
本文介绍了NSURL 路径与绝对字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多关于在 NSURLNSString 之间转换的问题.它们都涉及使用 NSString *path = [myURL absoluteString];NSString *path = [myURL path];.这些方法之间的实际区别是什么? 有没有什么时候应该使用一种方法而不是另一种方法?我尝试咨询Apple Docs,但我发现它不太有用.

我习惯于仅在有关网站和其他有关在不同机器之间发送信息的主题的讨论中提到 URL,而在仅处理单台机器上的文件结构时从未被提及.也许这就是我的一些困惑的来源,因为 NSURL 似乎是访问文件的首选方式,无论该文件是否存在于网络或本地设备上.或者也许这是一个完全不相关的话题.我什至不确定.

解决方案

问题 1:

<块引用>

这些方法之间的实际区别是什么?

让我们分析一下编写的 6 行代码 - 3 行用于本地 URL,3 行用于 http URL - 并稍微尝试一下.

让我们使用 file:// 方案创建一个 NSURL.如果你问自己为什么 file: 后面有 3 个 / 你应该记住一个完整的 URL 存在一个方案(file:// 和绝对或相对路径(您可以在 第 3 页的 RFC 1808 中找到有关创建 URL 的更多信息).我们使用以 / 开头的绝对路径,所以我们以 /// 结束.

NSURL *aLocalURL = [NSURL URLWithString:@"file:///Users/dennis/Desktop/"];NSLog(@"绝对字符串:%@", aLocalURL.absoluteString);NSLog(@"path: %@", aLocalURL.path);

输出:

<块引用>

绝对字符串:file:///Users/dennis/Desktop/
路径:/Users/dennis/Desktop

所以我们看到 absoluteString 仍然知道它的方案,而 path 不再有这个信息.

注意: path 是一个文件(目录)URL,作为 docs 状态,它的尾部斜杠被剥离.

<小时>

现在让我们看看远程 URL.大多数人对这些类型的 URL 更加熟悉.我们使用与本地 URL 相同的过程创建它.我们的方案现在是 http://,我们的 pathwww.apple.com/.

NSURL *anHTTPURL = [NSURL URLWithString:@"http://www.apple.com/"];NSLog(@"绝对字符串:%@", anHTTPURL.absoluteString);NSLog(@"path: %@", anHTTPURL.path);

输出:

<块引用>

绝对字符串:http://www.apple.com/
路径:/

再次,我们看到绝对字符串仍然知道它的方案,但 path 现在是 /.所以 path 在处理远程 URL 时似乎不是一个合适的方式.

然而,当我们有一个像 http://www.apple.com/index.html 这样的 URL 时,我们得到

<块引用>

绝对字符串:http://www.apple.com/index.html
路径:/index.html

阅读文档也有帮助:

<块引用>

根据 RFC 3986,权限(主机名和端口)部分后面的前导斜杠被视为路径的一部分.

所以 path 是在 authority 之后的斜杠处开始(并包括)的所有内容,在我们的例子中是 www.apple.com.

<小时>

问题 2

<块引用>

有什么时候应该使用一个而不是另一个?

来自 docs:(方法:path)

<块引用>

如果此 URL 对象包含文件 URL(由 isFileURL 确定),则此方法的返回值适合输入到 NSFileManager 或 NSPathUtilities 方法中.

在我看来,这句话清楚地指出,当您使用 NSFileManagerNSPathUtilities 时,您应该使用 path.

<小时>

结论:

当您使用远程 URL 时,您(通常)使用 absoluteString,否则结果不是您(通常)想要的.
当您使用本地 URL 时,请使用 path.

来源:
http://www.ietf.org/rfc/rfc1808.txt
http://www.ietf.org/rfc/rfc3986.txt
NSURL 类参考

I've seen many questions on SO concerning converting between NSURL and NSString. They all involve using either NSString *path = [myURL absoluteString]; or NSString *path = [myURL path];. What is the actual difference between these methods? Is there a time when one should be used over the other? I tried consulting the Apple Docs, but I found it less than helpful.

I'm used to URL's only being mentioned in discussions concerning websites and other topics regarding sending information between different machines, and never being mentioned when dealing with just the file structure on a single machine. Perhaps this is where some of my confusion is coming from, since NSURL seems to be the preferred way of accessing files, regardless of whether that file exists on a network or on the local device. Or maybe that's a totally unrelated topic. I'm not even sure.

解决方案

Question 1:

What is the actual difference between these methods?

Let's analyze this writing 6 lines of code - 3 for a local and 3 for http URL - and playing around with them a little bit.

Let's create an NSURL using the file:// scheme. If you ask yourself why there are 3 / after file: you should remember that a complete URL exists of a scheme (file:// and absolute or relative path (you can find more information on creating URLs in RFC 1808 on page 3). We use an absolute path which starts with a / so that we end up with ///.

NSURL *aLocalURL = [NSURL URLWithString:@"file:///Users/dennis/Desktop/"];
NSLog(@"absolute string: %@", aLocalURL.absoluteString);
NSLog(@"path: %@", aLocalURL.path);

Output:

absolute string: file:///Users/dennis/Desktop/
path: /Users/dennis/Desktop

So we see that absoluteString still knows its scheme whereas path doesn't have this information anymore.

Note: path is a file (directory) URL and as the docs state, the trailing slash it is stripped.


Now let's take a look at remote URLs. With these type of URLs most people are more familiar. We create it using the same procedure as for local URLs. Our scheme is now http:// and our path is www.apple.com/.

NSURL *anHTTPURL = [NSURL URLWithString:@"http://www.apple.com/"];  
NSLog(@"absolute string: %@", anHTTPURL.absoluteString);
NSLog(@"path: %@", anHTTPURL.path);

Output:

absolute string: http://www.apple.com/
path: /

Again, we see that the absolute string still knows its scheme but path is now /. So path seems to be not an appropriate way when working with remote URLs.

However, when we have an URL like http://www.apple.com/index.html we get

absolute string: http://www.apple.com/index.html
path: /index.html

Reading the docs helps here, too:

Per RFC 3986, the leading slash after the authority (host name and port) portion is treated as part of the path.

So the path is everything beginning (and including) at the slash after the authority which is www.apple.com in our case.


Question 2

Is there a time when one should be used over the other?

From the docs: (method: path)

If this URL object contains a file URL (as determined with isFileURL), the return value of this method is suitable for input into methods of NSFileManager or NSPathUtilities.

In my opinion that sentence states clearly that you should use path when you work with NSFileManager or NSPathUtilities.


Conclusion:

When you work with remote URLs you (generally) use absoluteString, otherwise the result is not what you (generally) want.
When you work with local URLs use path.

Sources:
http://www.ietf.org/rfc/rfc1808.txt
http://www.ietf.org/rfc/rfc3986.txt
NSURL Class Reference

这篇关于NSURL 路径与绝对字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆