Working with Files
The Foundation framework provides access to data stored in files in
several ways. All of the basic data classes have methods for
initializing objects from the contents of files, and for writing the
data represented by the object to a file. In addition to these
convenience facilities, Foundation provides two classes that provide
a much higher level of interaction with files and the filesystem:
NSFileManager
and NSFileHandle
.
The File Manager
The NSFileManager
class is an interface that applications
use to access and manipulate files and directories in the filesystem;
instances of NSFileManager
provide a doorway to
the filesystem for application developers. Several of
NSFileManager
’s methods call for
a handler
: argument. The handler is an object that
should implement fileManager:willProcessPath
: and
fileManager:shouldProceedAfterError
: methods.
These callback methods allow for error handling and confidence
testing with respect to the operation being performed. In Example 2-26, nil
is passed to
handler
: for the sake of clarity.
Additionally, methods that deal with movement around in the
filesystem and perform operations on files and directories typically
return a BOOL
value, to indicate an operation’s success or failure. Finally, methods that create new files or directories usually take a dictionary with file attributes as an argument. The attributes dictionary may take values to set the file’s owner, group owner, modification date, POSIX permissions; determine whether ...
Get Cocoa in a Nutshell 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.