Constructing and Manipulating Paths with NSPathUtilities

Revision as of 20:59, 11 November 2009 by Neil (Talk | contribs) (New page: In this final chapter on working with files and directories in Objective-C we will be covering the utilities provided by the Foundation Framework designed to aid in the process of working ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Revision as of 20:59, 11 November 2009 by Neil (Talk | contribs) (New page: In this final chapter on working with files and directories in Objective-C we will be covering the utilities provided by the Foundation Framework designed to aid in the process of working ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In this final chapter on working with files and directories in Objective-C we will be covering the utilities provided by the Foundation Framework designed to aid in the process of working with path names in a structured and platform neutral way.

The Anatomy of a Path

A path defines the location of a file in a file system. For example, a file located in the home directory of a user named John on a Mac OS X system will have the following path:

/Users/John/myfile.txt

In this example, the filename is myfile.txt and the rest of the path defines where in the directory structure the file in located.

The same file on a Linux system might have the following path:

/home/john/myfile.txt

Paths can also become quite convoluted. For example:

~demo/objc/code/../header/./mycode.h

The primary purpose of the NSUtilities is to make is easier to perform common tasks on a path. These utilities also provide a useful mechanism for finding a temporary directory, regardless of platform on which an Objective-C program is running.

Finding a Temporary Directory

Most operating systems have a standard directory provided specifically for the purposes of temporarily storing files. Other systems also provide a different temporary directory for each user. The exact location differ depending on the platform in question so in order to make your code compatible with as many platforms as possible it is unwise to make assumptions about the location of this temporary directory. Instead, it is much safer to use the NSTemporaryDirectory() to identify the appropriate directory. This function returns the temporary directory for the current user in the form of an NSString object.

The following code excerpt identifies and then displays the temporary directory using the NSTemporaryDirectory() function:

NSString *tempdir;

tempdir = NSTemporaryDirectory();

NSLog (@"Temp Dir = %@", tempdir);

Getting the Current User's Home Directory

Extracting the Filename from a Path

As previously discussed, a path can consist of the directory in which a file is located, followed by the name of the file. A common requirement when working with files when programming in any language is to extract just the file name from a path. This can easily be achieved using the lastPathComponent method:

NSString *samplepath = @"/Users/demo/objc/sample.m";
NSString *filename;

filename = [samplepath lastPathComponent];

NSLog (@"lastPathComponent = %@", filename);

When executed, the above code excerpt will display the filename part of the path, i.e. sample.m.

Extracting the Filename Extension

Filenames with characters after the final '.' character are said to have a filename extension. For example, the filename extension for a file named myfile.txt is txt. The filename extension may be extracted from a path using the pathExtension method:

NSString *samplepath = @"/Users/demo/objc/sample.m";
NSString *pathext;

pathext = [samplepath pathExtension];

NSLog (@"pathExtension = %@", pathext);

The above code will output the filename extension for the file referenced in the samplepath string, in this case m.

Standardizing a Path

It is surprising how quickly a path can become complicated, especially when allowing a user to navigate through a file system. The NSUtilities package provides the stringByStandardizingPath method. In the following code we will use this method to make some sense out of a path that reads as follows:

~demo/objc/code/../header/./../includes/mycode.h

NSString *samplepath = @"/Users/demo/objc/code/../header/./../includes/mycode.h";
NSString *cleanpath;

cleanpath = [samplepath stringByStandardizingPath];

NSLog (@"Standardized path = %@", cleanpath);

The output from the above code will report to us that the convoluted path resolves down to the much simpler:

/Users/demo/objc/includes/mycode.h

Extracting the Components of a Path

The pathComponents method extracts the various components that comprise a complete path and returns them in an NSArray:

NSString *samplepath = @"/Users/demo/objc/code/includes/mycode.h";
NSString *component;
NSArray *pathcomponents;

pathcomponents = [samplepath pathComponents];

for (component in pathcomponents)
        NSLog (@"component = %@", component);

When executed, the above code will produce the following output:

component = /
component = Users
component = demo
component = objc
component = code
component = includes
component = mycode.h