Swift 获取完整路径或转换为完整路径

Swift 获取完整路径或转换为完整路径,swift,file,path,enumerator,Swift,File,Path,Enumerator,使用时 let directoryEnumerator = FileManager().enumerator(at: ... 在Swift 3中,我从文件夹中获取所有文件,例如 "file:///Volumes/MacOS/fasttemp/Fotos/" 结果不包括前导路径(此处为“/Volumes/MacOS”)。所以我明白了 "file:///fasttemp/Fotos/2005/" 如何获取完整路径(直接从枚举器获取)或转换它们。我想使用URL函数,而不是通过假设操纵字符串函数

使用时

let directoryEnumerator = FileManager().enumerator(at: ...
在Swift 3中,我从文件夹中获取所有文件,例如

"file:///Volumes/MacOS/fasttemp/Fotos/"
结果不包括前导路径(此处为“/Volumes/MacOS”)。所以我明白了

"file:///fasttemp/Fotos/2005/"

如何获取完整路径(直接从枚举器获取)或转换它们。我想使用URL函数,而不是通过假设操纵字符串函数

请注意,您希望尽可能从以下位置使用
URL

URL对象是引用本地文件的首选方式。最 从文件读取数据或向文件写入数据的对象具有 接受NSURL对象而不是路径名作为文件引用

以下是如何从目录中获取所有对象的示例:

import Foundation

let manager = FileManager.default

// Get URL for the current user’s Documents directory
// Use URL instead of path, it’s more flexible and preferred
if let documents = manager.urls(for: .documentDirectory, in: .userDomainMask).first,

  // Get an Enumerator for the paths of all the objects in the directory
  // but do not descend into directories or packages
  let directoryEnumerator = manager.enumerator(at: documents, includingPropertiesForKeys: [URLResourceKey.pathKey], options: [.skipsSubdirectoryDescendants, .skipsPackageDescendants]) {

  // iterate through the objects (files, directories, etc.) in the directory
  for path in directoryEnumerator {
    print(path)
  }
}

请注意,您希望尽可能从以下位置使用
URL

URL对象是引用本地文件的首选方式。最 从文件读取数据或向文件写入数据的对象具有 接受NSURL对象而不是路径名作为文件引用

以下是如何从目录中获取所有对象的示例:

import Foundation

let manager = FileManager.default

// Get URL for the current user’s Documents directory
// Use URL instead of path, it’s more flexible and preferred
if let documents = manager.urls(for: .documentDirectory, in: .userDomainMask).first,

  // Get an Enumerator for the paths of all the objects in the directory
  // but do not descend into directories or packages
  let directoryEnumerator = manager.enumerator(at: documents, includingPropertiesForKeys: [URLResourceKey.pathKey], options: [.skipsSubdirectoryDescendants, .skipsPackageDescendants]) {

  // iterate through the objects (files, directories, etc.) in the directory
  for path in directoryEnumerator {
    print(path)
  }
}
如果“MacOS”是当前启动磁盘的名称,那么“/Volumes/MacOS”是指向“/”的符号链接,因此“/fastemp/Fotos/2005/”和“/Volumes/MacOS/fastemp/Fotos/”都是指向同一文件的绝对路径

为了获得唯一的文件名表示形式,您可以查询 其规范路径的URL。例如:

let url = URL(fileURLWithPath: "/Volumes/MacOS/Applications/Utilities/")
if let cp = (try? url.resourceValues(forKeys: [.canonicalPathKey]))?.canonicalPath {
    print(cp)
}
// Output: "/Applications/Utilities"
这需要macOS 10.12/iOS 10或更高版本。在较旧的系统上,您可以 使用
realpath()
系统调用:

if let rp = url.withUnsafeFileSystemRepresentation ({ realpath($0, nil) }) {
    let fullUrl = URL(fileURLWithFileSystemRepresentation: rp, isDirectory: true, relativeTo: nil)
    free(rp)
    print(fullUrl.path)
}
// Output: "/Applications/Utilities"
如果“MacOS”是当前启动磁盘的名称,那么“/Volumes/MacOS”是指向“/”的符号链接,因此“/fastemp/Fotos/2005/”和“/Volumes/MacOS/fastemp/Fotos/”都是指向同一文件的绝对路径

为了获得唯一的文件名表示形式,您可以查询 其规范路径的URL。例如:

let url = URL(fileURLWithPath: "/Volumes/MacOS/Applications/Utilities/")
if let cp = (try? url.resourceValues(forKeys: [.canonicalPathKey]))?.canonicalPath {
    print(cp)
}
// Output: "/Applications/Utilities"
这需要macOS 10.12/iOS 10或更高版本。在较旧的系统上,您可以 使用
realpath()
系统调用:

if let rp = url.withUnsafeFileSystemRepresentation ({ realpath($0, nil) }) {
    let fullUrl = URL(fileURLWithFileSystemRepresentation: rp, isDirectory: true, relativeTo: nil)
    free(rp)
    print(fullUrl.path)
}
// Output: "/Applications/Utilities"

“/Volumes/MacOS”是指向“/”的符号链接,因此“/fasttemp/Fotos/2005/”和“/Volumes/MacOS/fasttemp/Fotos/”都是指向同一文件的绝对路径。是否有函数获取文件的特定表示形式?我喜欢比较它们,例如将它们用作键。如果它们使用不同的表示形式作为字符串,则会失败。“/Volumes/MacOS”是指向“/”的符号链接,因此“/fasttemp/Fotos/2005/”和“/Volumes/MacOS/fasttemp/Fotos/”都是指向同一文件的绝对路径。是否有函数可以获取文件的特定表示形式?我喜欢比较它们,例如将它们用作键。如果他们使用不同的表示法作为字符串,那就失败了。这正是我的想法(我也这么做了)。我正在寻找一种将函数从长(显式)转换为短版本(仅“/”)的方法,而无需自己编写字符串。这正是我的想法(也是我所做的)。我正在寻找一个从长(显式)到短版本(仅“/”)的转换函数,而不需要我自己在字符串基础上编写它。谢谢!Ups,刚刚得到信息,该功能仅在OSX 10.12上可用!对于>=10.9,有类似的东西吗?是的,这很有效。该功能似乎只适用于文件夹,但我可以先删除文件名,进行转换,然后再次添加。多谢各位@彼得71:不客气。它也适用于我测试中的文件。您可能必须将isDirectory参数设置为false。很好,就是这样:“canonicalPath”。谢谢!Ups,刚刚得到信息,该功能仅在OSX 10.12上可用!对于>=10.9,有类似的东西吗?是的,这很有效。该功能似乎只适用于文件夹,但我可以先删除文件名,进行转换,然后再次添加。多谢各位@彼得71:不客气。它也适用于我测试中的文件。您可能必须将isDirectory参数设置为false。