Directory Enumeration

Processing the contents of a directory is a common operation. There are a number of NSFileManager calls, which can process a directory in different ways. Both the path-based and URL-based calls will get you the contents of a directory. The URL-based calls can also prefetch the metadata you want, speeding up the operation.

Enumeration with paths

You can get a flat listing of a directory in an array:

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

This returns an array of file names of all the files in the directory. It is flat because it does not recurse into subdirectories. You can get a deep listing that recurses into subdirectories with this call:

- (NSArray *) subpathsOfDirectoryAtPath ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.