Apple-Staaten etwa NSSearchPathForDirectoriesInDomains(_:_:_:)
:
Sie sollten mit den FileManager
Methoden urls(for:in:)
und url(for:in:appropriateFor:create:)
betrachten, welche URLs zurückgeben, die das sind bevorzugtes Format.
Mit Swift 3, FileManager
hat eine Methode namens contentsOfDirectory(at:includingPropertiesForKeys:options:)
. contentsOfDirectory(at:includingPropertiesForKeys:options:)
hat die folgende Deklaration:
Führt eine flache Suche im angegebenen Verzeichnis durch und gibt URLs für die enthaltenen Elemente zurück.
func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options mask: FileManager.DirectoryEnumerationOptions = []) throws -> [URL]
Deshalb, um die URLs der Dateien in Dokumentenverzeichnis enthalten abzurufen, können Sie das folgende Code-Schnipsel verwenden, die FileManager
‚s urls(for:in:)
und contentsOfDirectory(at:includingPropertiesForKeys:options:)
Methoden verwendet:
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])
// Print the urls of the files contained in the documents directory
print(directoryContents)
} catch {
print("Could not search for urls of files in documents directory: \(error)")
}
Als Beispiel zeigt die folgende Implementierung UIViewController
, wie Sie eine Datei speichern m App Bundle Dokumentenverzeichnis und wie die URLs der Dateien im Dokumentenverzeichnis gespeichert zu bekommen:
import UIKit
class ViewController: UIViewController {
@IBAction func copyFile(_ sender: UIButton) {
// Get app bundle file url
guard let bundleFileUrl = Bundle.main.url(forResource: "Movie", withExtension: "mov") else { return }
// Create a destination url in document directory for file
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let documentDirectoryFileUrl = documentsDirectory.appendingPathComponent("Movie.mov")
// Copy bundle file to document directory
if !FileManager.default.fileExists(atPath: documentDirectoryFileUrl.path) {
do {
try FileManager.default.copyItem(at: bundleFileUrl, to: documentDirectoryFileUrl)
} catch {
print("Could not copy file: \(error)")
}
}
}
@IBAction func displayUrls(_ sender: UIButton) {
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])
// Print the urls of the files contained in the documents directory
print(directoryContents) // may print [] or [file:///private/var/mobile/Containers/Data/Application/.../Documents/Movie.mov]
} catch {
print("Could not search for urls of files in documents directory: \(error)")
}
}
}
Sie wollen zeigen, die Namen der Dateien im Dokumentenverzeichnis oder die Dateien in den Ordnern? –
zuerst, nur Dateien im Dokumentenordner selbst! – pawi
Ich habe gerade Ihren Code ausgeführt und es hat wie erwartet funktioniert. Ich habe eine Liste von dem, was in meinem Dokumentenverzeichnis ist. Verstehen wir Ihre Frage falsch? – jwlaughton