Nayan Chauhan
Nayan Chauhan

Reputation: 542

Copy contents of directory to an NSArray

I have many images of jpg format in a folder named Wallpaper . I am trying to read the contents of directory and store them in an array using the following code :

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:@"/Wallpaper" error:NULL];

but its not working. I am unable to fetch them into the array. Can anyone tell me what's wrong with this ?

Upvotes: 1

Views: 323

Answers (3)

Sanchit Paurush
Sanchit Paurush

Reputation: 6152

Try This:

NSFileManager *filemgr= [NSFileManager defaultManager];

NSArray *filelist = [filemgr contentsOfDirectoryAtPath:Pathwithfoldername error:nil];

Upvotes: 0

fvwmer
fvwmer

Reputation: 301

You are using absolute path "/Wallpaper", but I guess your wallpaper folder is not in /?

NSArray *fileList = [manager contentsOfDirectoryAtPath:@"/Wallpaper" error:NULL];

And try it like this:

NSError *error = nil;
NSArray *fileList = [manager contentsOfDirectoryAtPath:@"/Wallpaper" error:&error];
NSLog(@"load wallpaper error: %@", error);

This will give you the actual error occurred.

Upvotes: 0

Mathew Varghese
Mathew Varghese

Reputation: 4527

You need to specify the complete path of the directory. I assume your folder Wallpaper is in the Documents Directory.

NSFileManager   *fileManager        =   [NSFileManager defaultManager];
NSString        *documentsPath      =   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString        *extractDirPath     =   [documentsPath stringByAppendingString: @"/Wallpaper"];
NSArray         *extractsList       =   [fileManager contentsOfDirectoryAtPath: extractDirPath error: nil];

Upvotes: 5

Related Questions