objective-c - 将 NSURL 转换为本地文件路径

标签 objective-c xcode macos

我有一个如下所示的 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, returns nil.

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/

相关文章:

ios - XCode 6 项目在 ios 7.1 上 segue 后崩溃

c++ - macOS : Correct programmatic way to determine volume info

iphone - 为 UITableView 添加阴影

ios - SCNNode(SCNCamera)旋转

objective-c - UItable 单元格溢出 ios

ios - 在 iOS 中保存和加载多个字符串到 UITextView

objective-c - Xcode 中是否有组合键来实现协议(protocol)?

ios - 将 SensibleTableView (STV) 的静态库与 cocoapods 一起使用

c - 在运行 OS X 10.12 的并行 VM 上显示内核打印输出中的指针值(而不是 <ptr>)

macos - FireMonkey/Rad Studio XE2 : How can I show the SaveDialog filter on OS X?