我有一个如下所示的 NSURL:
file://localhost/Users/myuser/myfile.txt
是否有标准函数可以将其转换为如下所示的本地文件路径:
/Users/myuser/myfile.txt
最佳答案
使用-[NSURL路径]
方法:
NSLog(@"%@", myUrl.path);
来自文档:
The path of the URL, unescaped with the
stringByReplacingPercentEscapesUsingEncoding:
method. If the receiver does not conform to RFC 1808, returnsnil
.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.Per RFC 3986, the leading slash after the authority (host name and port) portion is treated as part of the path.
请注意,您可以使用 +[NSURL fileURLWithPath:]
创建这样的 URL。
关于objective-c - 将 NSURL 转换为本地文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6956325/