I have the following structure inside my Xcode project in resource navigator which I want to reflect inside my app.
Namely, I want to "scan" inside the "Books" folder and get an NSArray
of all the folders in it. The next step is I want to get an NSArray
of all the files in each folder.
So far, I've tried using anything, that's connected to NSBundle
to get the list of folders, but this gives me wrong results:
NSLog(@"bundle path is %@", [[NSBundle mainBundle] bundlePath]);
NSLog(@"resource path is %@", [[NSBundle mainBundle] resourcePath]);
Both of these methods don't reflect the actual folder structure programmatically.
Is there any way we can do this?
As @rmaddy noted, you should actually have blue folders in your Xcode project and then use the NSDirectoryEnumerator
to get a complete list of all the folders.
Here is how I've solved this:
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"Books" isDirectory:YES];
NSDirectoryEnumerator *dirEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:bundleURL includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey,nil] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants errorHandler:nil];
for (NSURL *theURL in dirEnumerator){
// Retrieve the file name. From NSURLNameKey, cached during the enumeration.
NSString *folderName;
[theURL getResourceValue:&folderName forKey:NSURLNameKey error:NULL];
// Retrieve whether a directory. From NSURLIsDirectoryKey cached during the enumeration.
NSNumber *isDirectory;
[theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
if([isDirectory boolValue] == YES){
NSLog(@"Name of dir is %@", folderName);
}
}