如何获取iPhone App Documents Directory的目录树

时间:2011-09-11 05:31:14

标签: iphone

在我的应用程序中,我在Documents下创建了子文件夹,并在子文件夹下创建了子文件夹。所以有一个文档树。我希望以一种能够在tableview中列出它们的方式获取树。它们看起来像下面的线性列表,用不同的缩进来显示它们的级别。

enter image description here

当我尝试使用while循环和for循环时,我不确定是否有一些方便的方法来完成此操作。

感谢您的建议。

感谢。

3 个答案:

答案 0 :(得分:1)

Apple提供了许多用于文件系统访问的类,甚至还有一些用于您尝试进行递归的类。

我建议您查看the Apple File System Programming GuideNSFileManager class documentation。此外,NSDirectoryEnumerator class也可能有所帮助。

答案 1 :(得分:0)

我不知道如何一次获得整个层次结构。但是以下方法可能会有所帮助。

1使用此命令获取文件。

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString * documentPath = [paths objectAtIndex:0];

2使用- (NSArray *)contentsOfDirectoryAtPath:(NSString )path error:(NSError *)error 获取文档目录中的目录列表。

阅读此类引用以获取更多详细信息。

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

3使用以下方法可以创建内部目录的路径。

NSString * directoryPath = [documentPath stringByAppendingPathComponent:[MAIN_DIRECTORY stringByAppendingPathComponent:filePath]];

使用上面三个可以一起浏览文档目录的内容。

希望这是有帮助的

答案 2 :(得分:0)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];
    for (NSString *s in fileList){
       NSLog(s);
    }