Search code examples
iosobjective-carraysnsfilemanagernsbundle

Create Array of Strings from Group Contents


I have 3 xml files in a group folder within my project's supporting files. I'm trying to create an array that contains the filenames, as strings, for each file in the group's folder.

Example...Group Folder Named XLMS

XMLS
   one.xml
   two.xml
   three.xml

The way I have shown, only works if the files are in an actual folder (blue). How may I do the same for a group folder (yellowish)? Please help, thanks.

viewDidLoad

NSMutableString* bundlePath = [NSMutableString stringWithCapacity:4];

[bundlePath appendString:[[NSBundle mainBundle] bundlePath]];

[bundlePath appendString:@"/XMLS"];

NSArray *stringArray  = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];

[stringArray description];

Solution

  • Folder references will create a directory in the bundle, allowing you to figure out the contents. By using Groups, you cannot do this. If you insist on using the a Group, an option is to create a manifest that contains the names of the file you want.

    Note this is more error prone because you need to make sure the manifest contains the right filenames (i.e. if you change a filename, you'll need to make sure to update the manifest) AND you will also need to load the manifest first. Why don't you want to use Folder References in this case? If you make the manifest a plist you can automagically load it as an array doing something like:

    NSMutableArray *stringArray = [NSArray arrayWithContentsOfFile:manifestPath];