我的NSDocumentsDirectory中保存了一些文件,如何获取这些文件的名称,然后在uitableview中显示这些名称?

我只想将对象的名称检索为NSString,就可以将文件检索为对象,但不能检索其名称。

这是它的代码:

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSError * error;
objectsAtPathArray =  (NSMutableArray*)[[NSFileManager defaultManager]
                      contentsOfDirectoryAtPath:documentsDirectory error:&error];
[objectsAtPathArray removeObjectAtIndex:0];

最后一行是删除.DS_Store文件

最佳答案

试试这个,

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

    objectsAtPathArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy];

    for(int i = 0; i < [objectsAtPathArray count]; i++)
    {
       if (![[objectsAtPathArray objectAtIndex:i]isEqualToString:@".DS_Store"])
       {
         [NewArray addObject:[objectsAtPathArray objectAtIndex:i]];
          NSLog(@"NewArray=%@",[NewArray objectAtIndex:i]);
       }
     }

10-08 05:23