cit
cit

Reputation: 2605

Array not being populated as expected in iOS project

I have an iOS project that has some mp3 files in its Resources folder : alt text

I would like to add them to an array.

The following would appear to be the appropriate way to add the mp3's to an array, but when I list the array's contents it is empty? Not sure what I am missing:

NSArray *myArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:@"Resources"];

Running the following indicates the array is empty:

NSLog(@"Array contents: %@", myArray);

or

NSLog (@"Number of elements in array = %i", [myArray count]);

Upvotes: 0

Views: 356

Answers (2)

Michael Lowman
Michael Lowman

Reputation: 3068

The Resources directory in XCode is not where your files end up on the device. They are placed in the top level of the Application Library Directory. You can use this function to get the path:

NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];

You can also just use

NSArray *files = [[NSBundle bundle] pathsForResourcesOfType:@"mp3" inDirectory:nil];

Upvotes: 3

zoul
zoul

Reputation: 104065

Isn’t the problem simply the fact that the Resources folder is considered the root of the bundle? Did you try the same thing with . or something similar as the directory name?

Upvotes: 1

Related Questions