Constructing and Manipulating Paths with NSPathUtilities

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Working with Files in Objective-CCopying Objects in Objective-C

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print

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.


Contents


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

The home directory of the current user can be identified using the NSHomeDirectory() function. This function takes no arguments and returns an NSString object containing the path to the home directory of the user executing the program:

NSString *homedir;

homedir = NSHomeDirectory();

NSLog (@"Home directory of current user is %@", homedir);

Getting the Home Directory of a Specified User

The home directory of any user on a system can be obtained using the NSHomeDirectoryForUser() function. This function takes as its sole argument an NSString object containing the name of the user and returns another NSString object containing the corresponding home directory:

NSString *homedir;
NSString *username = @"Paul";

homedir = NSHomeDirectoryForUser(username);

NSLog (@"Home directory of user %@ is %@", username, homedir);

If the requested user does not exist on the system the function will return null.

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

Purchase the full edition of this Objective-C book in Print ($14.99) or eBook ($12.99) format
Objective-C 2.0 Essentials Print and eBook (ePub/PDF/Kindle) editions contain 31 chapters.

Buy Print



PreviousTable of ContentsNext
Working with Files in Objective-CCopying Objects in Objective-C