0

你好,我卡在这件事上,我需要循环某个dir我需要从中获取所有文件名并将路径作为变量获取的地方。

我知道如何创建一个循环,但要获取目录的内容,我不知道。

任何帮助表示赞赏,谢谢!

4

1 回答 1

3

试试这样:

// If your folder is a document.
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:  @"Documents"];
// else you can give your folder path as well, if you know 
// for example like this NSString *docsDir = @"user/desktop/testFolder"

NSFileManager *localFileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
NSString *file = nil;
NSData *fileContents = [NSData data];
while ((file = [dirEnum nextObject])) 
{
   NSLog(@"your file name%@",file); // This will give your filename
   // Now for getting file path follow below.
   // here we are adding path to filename.
   NSString *fileNamePath = [docsDir stringByAppendingPathComponent:file];
   NSLog(@"your fileNamePath%@",fileNamePath); // This will give your filename path
   fileContents = [NSData dataWithContentsOfFile:fileNamePath]; // This will store file contents in form of bytes

}

希望能帮助到你:)

于 2013-09-08T20:26:14.330 回答