Reputation: 3751
I have the following code:
NSString *folderPath = [NSString stringWithFormat:@"%@/Objects/", [[NSBundle mainBundle] bundlePath]];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error: nil];
for (NSString *s in fileList) {
NSLog(s);
}
There are 100's of png files in the Objects folder.
However file list seems to be empty. What are something I can check about the setup of my project? Am I doing the code wrong? Should fileList be an array of all the pictures in Objects?
Im trying to obtain an array with the name of all images that I have so that I can assign and image to a UIImage later on
Upvotes: 2
Views: 2143
Reputation: 34263
The yellow folder icon for your "Objects" folder indicates that you created a folder group instead of a folder reference (those have blue folder icons) when you dragged your image folder onto the Xcode project.
Xcode copies files in a folder group into the root directory of your app bundle during the "Copy Bundle Resources" build phase.
If you want a subdirectory named "Objects" in your app bundle, you have to choose "Create folder references for any added folder" after dragging "Objects" into your Xcode window.
You projects sidebar should display the "Objects" folder as follows:
To get a list of all .pngs from that copied folder, you can use the following code:
NSURL* resourceURL = [[NSBundle mainBundle] resourceURL];
resourceURL = [resourceURL URLByAppendingPathComponent:@"Objects"];
NSError* error = nil;
NSArray* resourceURLContents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:resourceURL includingPropertiesForKeys:nil options:0 error:&error];
resourceURLContents = [resourceURLContents filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL* evaluatedObject, NSDictionary *bindings) {
return [[evaluatedObject pathExtension] isEqualToString:@"png"];
}]];
NSLog(@"Contents:%@", resourceURLContents);
Upvotes: 12