Get fullpath or convert to fullpath

后端 未结 2 1007
情歌与酒
情歌与酒 2020-12-22 09:09

When using

let directoryEnumerator = FileManager().enumerator(at: ...

in Swift 3, I get all files from the folder, e.g.

\"f         


        
2条回答
  •  不思量自难忘°
    2020-12-22 09:53

    Note that you want to use URL wherever possible, from the NSURL documentation:

    URL objects are the preferred way to refer to local files. Most objects that read data from or write data to a file have methods that accept an NSURL object instead of a pathname as the file reference.

    Here’s an example of how to get all the objects from a directory:

    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)
      }
    }
    

提交回复
热议问题