- 4.1 General Concepts
- 4.2 Core Foundation Types
- 4.3 Basic Data Types
- 4.4 Collections
- 4.5 Enumeration
- 4.6 Property Lists
- 4.7 Interacting with the Filesystem
- 4.8 Notifications
- 4.9 Summary
4.7 Interacting with the Filesystem
Most nontrivial programs need to interact with the filesystem in some way. On most UNIX-like systems, including OS X, the filesystem is the only persistent storage facility provided. User defaults is just a high-level interface to a small part of the filesystem, providing access to specific files via a dictionary-like interface.
How you want to interact with the filesystem depends a lot on the task at hand. Cocoa provides a number of facilities exposing files as UNIX-style streams of bytes, or as structured data of some kind. Which you should use depends on your requirements.
4.7.1 Bundles
Bundles are a very important part of OS X. They were used on NeXT systems and have gradually replaced resource forks from earlier versions of Mac OS. The big advantage is not needing any special filesystem support.
Applications on OS X are bundles and can have other resources as well as the code. On NeXT systems, application bundles were used to store different versions of the executable for different platforms; you could have a single .app on an NFS share and run it on OPENSTEP, Solaris, or any other platform that it supported. This legacy is still found in OS X today. The binary is in the Contents/MacOS directory inside the bundle. In theory, you could add binaries for other platforms, although this is not currently supported by the Apple tools.
Prior to the release (and naming) of OS X, the in-development successor to Classic MacOS was called Rhapsody. Three "boxes" were announced by Apple. Two eventually became part of OS X. Blue box was the virtualized compatibility layer for MacOS that was called Classic on early versions of OS X and is not present on Intel Macs. The yellow box was the OpenStep environment that was later rebranded Cocoa. The final box, the red box, never made it to a shipping product and was a Windows environment for OS X similar to WINE. There was also a planned Windows version of the yellow box, based on the OPENSTEP Enterprise (OSE) product from NeXT, including Project Builder and Interface Builder and allowing Cocoa applications to be developed for Windows.
It seems likely that Apple still maintains descendants of the Windows version of the yellow box internally and uses them for porting applications like Safari to Windows, although Apple does not use the bundle architecture for these applications. Although the red box was not shipped, it was seen as a possible future product for long enough for OS X to retain the ability to run application bundles with executables in entirely different formats.
OS X, like OPENSTEP, uses the Mach-O binary format, which supports different format executables in the same binary files (sharing constants and data when the endian is the same). This is more efficient than having independent binaries for each version and allows Intel and PowerPC, 32-bit and 64-bit executables to be included in the same file. NeXT called these fat binaries, while Apple opted for the more politically correct universal binaries.
Because applications are bundles, every application has at least one bundle that it will want to load resources from. In a very simple application this happens automatically. The main nib for the application will be loaded when it starts and connected to the application delegate and any other objects.
Other resources can be loaded from the application bundle with the NSBundle class. In general, you will have one instance of this class for each bundle you want to interact with. You can get the application bundle with
[NSBundle mainBundle];
Be careful when doing this. At some point in the future you may decide that your class is very useful and that you want to reuse it. When you do this, you will move it and a framework—another kind of bundle containing a loadable library, headers, and resources—along with any resources it might want to load. When you get the main bundle from your class, you will get the application bundle for the application that linked against the framework, rather than the framework bundle. If you are getting a bundle to load resources that are included with the class then this is not what you want. Instead, you should use
[NSBundle bundleForClass: [self class]];
This is relatively slow, so it is best done in the +initialize method for the class and cached in a file-static variable, like this:
static NSBundle *frameworkBundle; + (void) initialize { frameworkBundle = [[NSBundle bundleForClass: self] retain]; }
In real code, you would probably want to wrap this in a check to ensure that it was only being called on the correct class, as shown in Chapter 3. Because this is a class method, it only needs to pass self, rather than [self class] as the parameter. You can also use +bundleWithIdentifier, which is generally faster. This loads the bundle that has the identifier provided as the argument. The bundle identifier is set in the bundle's property list by the CFBundleIdentifier key.
Once you have a bundle, you can load resources from it. This is a two-step process. The first is to find the path of the resource, using a method like this:
- (NSString*)pathForResource: (NSString*)name ofType: (NSString*)extension inDirectory: (NSString*)subpath forLocalization: (NSString*)localizationName
There are two wrapper versions of this method where the last parameters are filled in with default values. The simplest form just has the first two and finds resources using the user's preferred localization in the top-level resource directory in the bundle.
If you want to load all of the resources of a specific type in a bundle, there is a form that returns an array instead of a string:
- (NSArray*)pathsForResourcesOfType: (NSString*)extension inDirectory: (NSString*)subpath
This, and the version that specifies a localization, finds all of the resources of a specific type, for example, all of the png files in a theme directory in the Resources directory in the bundle.
In addition to resources, you can load code from bundles, too. Listing 4.16 shows a simple framework loader. Because frameworks are just another kind of bundle with a well-known layout, the standard bundle loading code can be used to load them.
This example is taken from Étoilé's LangaugeKit and is used to allow scripts loaded and compiled by a running program to specify frameworks that they depend upon, without requiring the program that loads them to link against every possible framework that a script might want.
This example shows a number of Cocoa features. The first is the file manager, which we will look at in the next section. This is used in line 24 to test whether the framework exists at a given path. If it does, then NSBundle is used on lines 27 and 28 to load the code in the framework.
Listing 4.16. A simple framework loader. [from: examples/Loader/simpleLoader.m]
7| @implementation SimpleLoader 8| + (BOOL) loadFramework: (NSString*)framework 9| { 10| NSFileManager *fm = [NSFileManager defaultManager]; 11| NSArray *dirs = 12| NSSearchPathForDirectoriesInDomains( 13| NSLibraryDirectory, 14| NSAllDomainsMask, 15| YES); 16| FOREACH(dirs, dir, NSString*) 17| { 18| NSString *f = 19| [[[dir stringByAppendingPathComponent: @"Frameworks"] 20| stringByAppendingPathComponent: framework] 21| stringByAppendingPathExtension: @"framework"]; 22| // Check that the framework exists and is a directory. 23| BOOL isDir = NO; 24| if ([fm fileExistsAtPath: f isDirectory: &isDir] 25| && isDir) 26| { 27| NSBundle *bundle = [NSBundle bundleWithPath: f]; 28| if ([bundle load]) 29| { 30| NSLog(@"Loaded_bundle_%@", f); 31| return YES; 32| } 33| } 34| } 35| return NO; 36| } 37| @end
The function on line 11 is one of the most useful, and most overlooked, parts of Cocoa, since it allows you to avoid hard-coding paths in a lot of instances. Line 19 shows some of NSString's path manipulation code. This is used to assemble the correct path by appending the Frameworks directory, then the framework name as path components, and then the .framework extension. This could be done with -stringWithFormat: for OS X, but doing it this way means that it will continue to work if you try to move your code to a different platform with different path formats.
4.7.2 Workspace and File Management
Cocoa provides two ways of interacting with the filesystem, NSFileManager and NSWorkspace. The latter is part of AppKit and provides a higher-level interface. The NSWorkspace class does file operations in the background and posts a notification when they are done, while NSFileManager works synchronously. Both classes are singletons; you will only ever have (at most) one instance for each in an application.
We saw an example of one of the things you can do with a file manager in Listing 4.16. This used the -fileExistsAtPath:isDirectory: method, to see if a file existed. The second argument to this is a pointer to a BOOL, which is set to YES if the file is found and is a directory.
Most other common file manipulation operations are supported by the file manager, such as copying, moving, and linking files and directories. It can also enumerate the contents of folders and compare files. Most of NSFileManager's functionality is exposed by a single method in NSWorkspace:
- (BOOL)performFileOperation: (NSString*)operation source: (NSString*)source destination: (NSString*)destination files: (NSArray*)files tag: (NSInteger)tag
This takes a source and destination directory as arguments and an array of files. It performs move, copy, link, destroy, or recycle operations and sets the value of the integer pointed to by tag to indicate whether the operation succeeded.
Most of NSWorkspace's functionality deals with higher-level operations on files. While NSFileManager is for dealing with files as UNIX-style streams of bytes, NSWorkspace is for dealing with files as a user-level abstraction, representing documents or applications. Methods like openFile: are examples of this. This method opens a specified file with the default application and is used to implement the command-line open tool.
The low-level file manager methods are very easy to use. Listing 4.17 shows a simple tool for copying a file. This uses the user defaults system to read command-line arguments and then uses the file manager to copy the specified file.
Note that this example code does not check whether the input file exists or that the output is a valid destination. The file manager will call a delegate method in case of an error, but we did not set a handler on line 12, and so this will not allow error checking either. Implementing the handler is not required, it simply allows you to track the progress of the operation and to decide whether to proceed in case of an error. The return value from this method is a boolean indicating whether the copy succeeded. You can run this simple tool like this:
$ gcc -framework Foundation FileCopy.m -o FileCopy $ ./FileCopy -in FileCopy -out CopyOfFileCopy $ ls CopyOfFileCopy FileCopy FileCopy.m
Note that the file manager automatically resolved relative paths. These are treated as being relative to whatever the file manager returns from -currentDirectoryPath. You can alter the working directory for a running program by sending the file manager a -changeCurrentDirectoryPath: message. The working directory is much more important for command-line tools than it is for graphical applications. A command-line tool inherits its current working directory from the shell. The concept of a current directory does not make sense for an application invoked via the Finder or from the Dock.
Listing 4.17. A simple tool for copying files. [from: examples/FileCopy/FileCopy.m]
1| #import <Foundation/Foundation.h> 2| 3| int main(void) 4| { 5| [NSAutoreleasePool new]; 6| NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 7| NSString *source = [defaults stringForKey: @"in"]; 8| NSString *destination = [defaults stringForKey: @"out"]; 9| NSFileManager *fm = [NSFileManager defaultManager]; 10| [fm copyPath: source 11| toPath: destination 12| handler: nil]; 13| return 0; 14| }
Starting with 10.5, Apple began using the uniform type identifier (UTI) system for identifying file types. A UTI is a hierarchical arrangement of types. NSWorkspace is used to map between file extensions and UTIs.
4.7.3 Working with Paths
When working with filesystem paths, there are a number of helpful methods provided by NSString. These allow you to decompose paths into components and create various individual components without worrying about the kind of path that is represented.
On UNIX platforms, a tilde (~) is commonly used as a shorthand for the user's home directory. You can get this explicitly by calling NSHomeDirectory() but often users will enter strings containing this shorthand and expect it to work. If you select Go to Folder in the Finder's Go menu, then enter "~/Documents" then it will open a new window showing the Documents folder in your home directory.
The NSString class provides a convenient method for strings that may contain a tilde. If you send the string a -stringByExpandingTildeInPath message, then you will get back a new string containing the absolute path, without a tilde. Although less useful, it is also possible to go the other way by sending a full path a -stringByAbbreviatingWithTildeInPath message. If the path points to something inside the user's home directory, then it will be collapsed to only use a tilde character for this part of the path.
When interacting with the filesystem, you very often need to decompose a path into three parts: the file name, the file extension, and the path of the directory containing the file. You can do all of these from NSString, like this:
NSString *fullPath = @"/tmp/folder/file.extension"; // ext = @"extension"; NSString *ext = [fullPath pathExtension]; // file = @"file"; NSString *file = [[fullPath lastPathComponent] stringByDeletingPathExtension]; // dir = @"/tmp/folder"; NSString *dir = [fullPath stringByDeletingLastPathComponent];
There are also methods for constructing a path from individual parts, including appending components and setting the extension. Before you start writing code for parsing path strings yourself, make sure that NSString doesn't already have methods for doing what you need.
4.7.4 File Access
While NSFileManager lets you interact with the filesystem, and NSWorkspace lets you open files in other applications, neither provides a means of accessing the contents of files.
There is nothing stopping you from using the C library and POSIX functions for doing this, but they are not very convenient. Cocoa includes a wrapper around them in the form of the NSFileHandle class. This wraps a file handle as would be returned by open(). Four singleton instances exist, representing the C standard input, output and error streams, and a placeholder that discards all data written to it. Additional file handles can be created for reading, writing, or updating, with named constructors.
You can use the NSFileHandle class anywhere you have a C file handle using this initializer:
- (id)initWithFileDescriptor: (int)fileDescriptor closeOnDealloc: (BOOL)flag
The file descriptor is any C file descriptor, for example, the kind returned by open() or socket(). Whether the resulting object supports reading or writing depends on the underlying file descriptor. If you set flag to YES, then you can use this as a simple way of tracking how many parts of your program are using the file and ensuring that it is closed at the correct point. As long as you then only use the file descriptor with the object, it will stay open as long as some parts of your program are using it and close itself when it is no longer required.
If all you want to do is read data from a file, NSData objects can be created from a file using this method:
+ (id)dataWithContentsOfFile: (NSString*)path options: (NSUInteger)mask error: (NSError**)errorPtr
This creates a new NSData object from the contents of the file pointed to by path and sets the errorPtr to an NSError instance if it fails. The mask parameter allows two options to be set: NSMappedRead and NSUncachedRead. The first uses mmap() instead of file reading operations. As discussed earlier, this is a good idea if you know that the file is not going to be modified, for example, for read-only resources in an application bundle. If the system is low on memory, it can very cheaply free mapped data and load it back from the original file, while data read in will have to be written out to the swap file, even if they have not been modified. The second option allows the data to bypass the operating system's cache. If you know that you are just going to read the data once and then discard it, then it can improve performance. Otherwise it will use less RAM, but require more disk accesses.
NSData can also be used to write data back to a file. For small file output, using an NSMutableData to construct the file in memory and then using the writeToFile:atomically: or writeToURL:atomically: methods to output it is very simple. The second parameter to each of these is a BOOL, which, if set to YES, will write the data first to a temporary file and then rename the temporary file, ensuring on-disk consistency.