将NSURL转换为本地文件路径 [英] Convert NSURL to local file path
问题描述
我有一个看起来像这样的NSURL:
I have an NSURL that looks like this:
file://localhost/Users/myuser/myfile.txt
是否有一个标准函数将其转换为如下所示的本地文件路径:
Is there a standard function to convert it to a local file path looking like this:
/Users/myuser/myfile.txt
推荐答案
使用-[NSURL path]
方法:
NSLog(@"%@", myUrl.path);
从文档中:
URL的路径,未使用
stringByReplacingPercentEscapesUsingEncoding:
方法进行转义.如果接收方不符合RFC 1808,则返回nil
.
The path of the URL, unescaped with the
stringByReplacingPercentEscapesUsingEncoding:
method. If the receiver does not conform to RFC 1808, returnsnil
.
如果此URL对象包含文件URL(由isFileURL
确定),则此方法的返回值适合于输入到NSFileManager或NSPathUtilities的方法中.如果路径中带有斜线,则将其删除.
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. If the path has a trailing slash it is stripped.
根据RFC 3986,授权(主机名和端口)部分之后的斜杠被视为路径的一部分.
Per RFC 3986, the leading slash after the authority (host name and port) portion is treated as part of the path.
请注意,您可以使用+[NSURL fileURLWithPath:]
创建这样的URL.
Note that you can create such a URL with +[NSURL fileURLWithPath:]
.
这篇关于将NSURL转换为本地文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!