firmy Apple twierdzi o NSSearchPathForDirectoriesInDomains(_:_:_:)
:
Należy rozważyć zastosowanie metod urls(for:in:)
FileManager
i url(for:in:appropriateFor:create:)
które zwracają adresy URL, które są preferowany format.
szybkimi 3 FileManager
ma metodę zwaną contentsOfDirectory(at:includingPropertiesForKeys:options:)
. contentsOfDirectory(at:includingPropertiesForKeys:options:)
ma następującą deklarację:
Wykonuje płytkie wyszukiwanie określonego katalogu i zwraca adresy URL zawartych elementów.
func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options mask: FileManager.DirectoryEnumerationOptions = []) throws -> [URL]
Dlatego też, w celu pobrania adresów URL z plików znajdujących się w katalogu dokumentów, można użyć następującego fragmentu kodu, który używa FileManager
„s urls(for:in:)
i contentsOfDirectory(at:includingPropertiesForKeys:options:)
metody:
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)")
}
Poniżej przykładowa implementacja UIViewController
pokazuje, jak zapisać plik z m app pakiet do katalogu dokumentów i jak uzyskać adresy URL pliki zapisane w folderze Dokumenty:
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)")
}
}
}
chcesz pokazać również nazwy plików znajdujących się w katalogu dokumentów lub pliki znajdujące się w folderach? –
najpierw tylko pliki w folderze dokumentu! – pawi
Po prostu uruchomiłem twój kod i działało zgodnie z oczekiwaniami. Mam listę tego, co jest w moim katalogu dokumentów. Czy źle rozumiemy twoje pytanie? – jwlaughton