Objective c -enumaeratorAtPath:不在根级文件夹中工作

时间:2015-05-21 06:16:33

标签: objective-c macos nsfilemanager

我试图在根级别枚举文件和文件夹

 (/Users/Myname/Desktop or /Users/Myname/Documents),[enumarator nextobject] always gives nil value.

我该如何解决这个问题。

NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:@"/Users/Myname/Desktop"];

NSString *content=  [enumerator nextObject];//content always nil.

1 个答案:

答案 0 :(得分:1)

试试这个:

 NSFileManager *fileManager = [NSFileManager defaultManager];
 NSDirectoryEnumerator * emuerator = [fileManager enumeratorAtPath:@"/usr/Myname/Desktop"]; 
 NSString *filename; while ((filename = [emuerator 
 nextObject])) 
   {
    NSLog (@"file!  %@", filename); 
   }

OR

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSFileManager *manager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *emuerator= [manager enumeratorAtPath: @"/usr/Myname/Desktop"];


for (NSString *filename in emuerator) {
    // Do something with file
}

[manager release];