Как получить все пути для файлов в папке "Документы"?
Обычно я получаю пути с помощью NSBundle
.
Но NSBundle
не содержит папку "Документы".
Как получить все пути (или имена) для файлов в каталоге документов?
Ответы
Ответ 1
Это даст вам пути для всех файлов в каталоге документов
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files array %@", filePathsArray);
Вы можете получить путь для каждого объекта в файлеPathsArray, используя приведенный ниже код
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]];
Ответ 2
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Чтобы получить файлы из этого, вы должны указать структуру пути...
NSString *fileName = @"default.png";
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"%@", fileName];
folderPath вернет вам местоположение указанного имени файла.
Ответ 3
Здесь быстро.
Print statement напечатает все доступные пути
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print("Paths - \(paths)")
let documentsDirectory = paths[0]
return documentsDirectory
}