O'Reilly logo

Advanced Mac OS X Programming: The Big Nerd Ranch Guide by Mark Dalrymple

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required