cannyboy
cannyboy

Reputation: 24426

Can you build an array of items from folder contents?

Let's say I've got a folder in my project with bunch of png files in it (or mp3 files, or mov files etc).

I would like to create an NSArray populated with the items within that folder... for instance an array of UIImages for the folder of images (or just an array of NSStrings of the image name).

Is this possible? I know I could create a plist with all the names in, or name the items individually within the code, but this would break the DRY (don't repeat yourself) rule. Has anyone worked this out?

Upvotes: 3

Views: 233

Answers (2)

Yuji
Yuji

Reputation: 34185

Or, if your resources are in the app bundle, use -[NSBundle pathsForResourcesOfType:inDirectory:] as in

 NSArray* mp3s=[[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:nil];

Passing nil to the directory parameter defaults to the top level in the app bundle on iPhone, Resources/ in the OS X.

Upvotes: 1

makdad
makdad

Reputation: 6450

Try NSFileManager's

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

Upvotes: 5

Related Questions