Search code examples
iphoneiosfilenamesnsdocumentdirectory

How to get the names of the files stored in NSDocumentsDirectory?


I have some files saved in my NSDocumentsDirectory, how do i get the names of those files and then display those names in a uitableview?

I just want to retrieve the name of the objects as NSString, I am able to retrieve the files as objects but not their names.

Here is the code for it:

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

NSString *documentsDirectory = [paths objectAtIndex:0];

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

Last line is to remove the .DS_Store file


Solution

  • adding a (for in) loop did the trick for me,

    for (NSString *fileName in self.objectsAtPathArray){
    
       cell.textLabel.text = fileName;
    }