xcode 如何获取文档目录中文件的所有路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7440662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get All paths for files in Documents directory?
提问by ChangUZ
I usually get paths by using NSBundle
.
我通常使用NSBundle
.
But, NSBundle
does not contain Documents folder.
但是,NSBundle
不包含 Documents 文件夹。
How to get All paths(or names) for files in Documents directory?
如何获取 Documents 目录中文件的所有路径(或名称)?
回答by sElanthiraiyan
This will give u the paths for all files under documents directory
这将为您提供文档目录下所有文件的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files array %@", filePathsArray);
You can get the path for each object in filePathsArray by using the below code
您可以使用以下代码获取 filePathsArray 中每个对象的路径
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]];
回答by alloc_iNit
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
To get files from that, you have to give the path structure...
要从中获取文件,您必须提供路径结构...
NSString *fileName = @"default.png";
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"%@", fileName];
The folderPathwill return you the location of given file name.
该FOLDERPATH将返回给定文件名的位置。
回答by Krunal
Here is in swift.
Print statement will print all available paths
这里是快速的。
打印语句将打印所有可用路径
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print("Paths - \(paths)")
let documentsDirectory = paths[0]
return documentsDirectory
}