2013-03-15 68 views
1

我无法显示目录中的所有文件。我确实在所有应用程序文件的相同文件夹中创建了一个名为“保存”的目录。contentsOfDirectoryAtPath返回null(iOS)

我用这个代码:

NSError *error = nil; 
NSArray *imageFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"saves" error: &error]; 

NSLog(@"FILES: %@", imageFiles); 

谁能帮助我,因为这段代码记录“空” - 所以假装没有在文件夹中任何东西,但该文件夹中我做了一个目录,两个空文件。

+0

检查错误消息,并张贴。 添加NSLog(@“%@”,error.localizedDescription) – damithH 2013-09-13 10:02:18

回答

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

检查测试目录中的文件:

NSArray *filePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[documentDirPath stringByAppendingPathComponent:@"test"] error:nil]]; 
+0

我想获取该目录中所有文件的数组,因此如果有3个文本文件,请显示该3个文本文件的名称。 – sjors 2013-03-15 12:43:27

+0

@sjors上面的代码会给你所有在目录中的文件的路径:) – 2013-03-15 12:44:13

+0

NSString * documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];你可以使用NSArray * AllFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[documentDirPath stringByAppendingPathComponent:@“saves”] error:nil]; NSLog(@“FilesTree%@”,AllFiles);对我返回null。 – sjors 2013-03-15 13:23:28

0

它看起来对我来说,你想保存在您的主束的文件。你是在模拟器上还是在实际设备上执行此操作?您无法将文件保存在设备上的主包中。

0
NSError *error = nil; 
NSArray *imageFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"saves" error: &error]; 


Not sure what ypou expect this to do... 
NSLog(@"FILES: %@", imageFiles); 

try this 
NSLog(@"Image file count %d", [imageFiles count]); 

for (int i - 0; i <= [imageFiles count] - 1; i++) 
{ 
    NSLog(@"file name : %@", [imageFiles objectAtIndex:i]); 
} 

代码未经测试,但我认为你的想法...