One of the most rewarding experiences you can have as a developer is to teach young people how to program. If you ever grow jaded by how fundamentally broken all software is, there’s nothing like watching a concept like recursion click for the first time to offset your world-weariness.
My favorite way to introduce the concept of programming is to set out all the ingredients for a peanut butter and jelly sandwich and ask the class give me instructions for assembly as if I were a robot 🤖. The punchline is that the computer takes every instruction as literally as possible, often with unexpected results. Ask the robot to “put peanut butter on the bread”, and you may end up with an unopened jar of Jif flattening a sleeve of Wonder Bread. Forget to specify which part of the bread to put the jelly on? Don’t be surprised if it ends up on the outer crust. And so on. Kids love it.
The lesson of breaking down a complex process into discrete steps
is a great encapsulation of programming.
And the malicious compliance from lack of specificity
echoes the analogy of “programming as wish making”
from our article about numericCast(_:)
.
But let’s take the metaphor a step further,
and imagine that instead of commanding a single robot to
(sudo
)
make a sandwich,
you’re writing instructions for a thousand different robots.
Big and small, fast and slow;
some have 4 arms instead of 2,
others hover in the air,
maybe a few read everything in reverse.
Consider what would happen if multiple robots tried to make a sandwich
at the same time.
Or imagine that your instructions
might be read by robots that won’t be built for another 40 years,
by which time peanut butter is packaged in capsules
and jelly comes exclusively as a gas.
That’s kind of what it’s like to interact with a file system.
The only chance we have at making something that works
is to leverage the power of abstraction.
On Apple platforms,
this functionality is provided by the Foundation framework
by way of File
.
We can’t possibly cover everything there is to know about working with file systems in a single article, so this week, let’s take a look at the operations you’re most likely to perform when building an app.
File
offers a convenient way to create, read, move, copy, and delete
both files and directories,
whether they’re on local or networked drives or iCloud ubiquitous containers.
The common currency for all of these operations are paths and file URLs.
Paths and File URLs
Objects on the file system can be identified in a few different ways. For example, each of the following represents the location of the same text document:
- Path:
/Users/NSHipster/Documents/article.md
- File URL:
file:///Users/NSHipster/Documents/article.md
- File Reference URL:
file:///.file/id=1234567.7654321/
Paths are slash-delimited (/
) strings that designate a location
in the directory hierarchy.
File URLs are URLs with a file://
scheme in addition to a file path.
File Reference URLs identify the location of a file
using a unique identifier separate from any directory structure.
Of those,
you’ll mostly deal with the first two,
which identify files and directories using a relational path.
That path may be absolute
and provide the full location of a resource from the root directory,
or it may be relative
and show how to get to a resource from a given starting point.
Absolute URLs begin with /
,
whereas relative URLs begin with
./
(the current directory),
../
(the parent directory), or
~
(the current user’s home directory).
File
has methods that accept both paths and URLs —
often with variations of the same method for both.
In general, the use of URLs is preferred to paths,
as they’re more flexible to work with.
(it’s also easier to convert from a URL to a path than vice versa).
Locating Files and Directories
The first step to working with a file or directory
is locating it on the file system.
Standard locations vary across different platforms,
so rather than manually constructing paths like /System
or ~/Documents
,
you use the File
method url(for:in:appropriate
to locate the appropriate location for what you want.
The first parameter takes one of the values specified by
FileManager.SearchPathDirectory
.
These determine what kind of standard directory you’re looking for,
like “Documents” or “Caches”.
The second parameter passes a
FileManager.SearchPathDomainMask
value,
which determines the scope of where you’re looking for.
For example,
.application
might refer to /Applications
in the local domain
and ~/Applications
in the user domain.
letdocumentsDirectoryURL=tryFileManager.default.url(for:.documentDirectory,in:.userDomainMask,appropriateFor:nil,create:false)
NSFileManager*fileManager=[NSFileManagerdefaultManager];NSString*documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];NSString*filePath=[documentsPathstringByAppendingPathComponent:@"file.txt"];
Determining Whether a File Exists
You might check to see if a file exists at a location before trying to read it,
or want to avoid overwriting an existing one.
To do this, call the file
method:
letfileURL:URLletfileExists=FileManager.default.fileExists(atPath:fileURL.path)
NSURL*fileURL;NSFileManager*fileManager=[NSFileManagerdefaultManager];BOOLfileExists=[fileManagerfileExistsAtPath:[fileURLpath]];
Getting Information About a File
The file system stores various pieces of metadata
about each file and directory in the system.
You can access them using the File
method attributes
.
The resulting dictionary contains attributes keyed by File
values,
including .creation
:
letfileURL:URLletattributes=FileManager.default.attributesOfItem(atPath:fileURL.path)letcreationDate=attributes[.creationDate]
NSURL*fileURL;NSFileManager*fileManager=[NSFileManagerdefaultManager];NSError*error=nil;NSDictionary*attributes=[fileManagerattributesOfItemAtPath:[fileURLpath]error:&error];NSDate*creationDate=attributes[NSFileCreationDate];
Listing Files in a Directory
To list the contents of a directory,
call the File
method
contents
.
If you intend to access any metadata properties,
as described in the previous section
(for example, get the modification date of each file in a directory),
specify those here to ensure that those attributes are cached.
The options
parameter of this method allows you to skip
hidden files and/or descendants.
letdirectoryURL:URLletcontents=tryFileManager.default.contentsOfDirectory(at:directoryURL,includingPropertiesForKeys:nil,options:[.skipsHiddenFiles])forfileincontents{// ...}
NSFileManager*fileManager=[NSFileManagerdefaultManager];NSURL*bundleURL=[[NSBundlemainBundle]bundleURL];NSArray*contents=[fileManagercontentsOfDirectoryAtURL:bundleURLincludingPropertiesForKeys:@[]options:NSDirectoryEnumerationSkipsHiddenFileserror:nil];NSPredicate*predicate=[NSPredicatepredicateWithFormat:@"pathExtension == 'png'"];for(NSURL*fileURLin[contentsfilteredArrayUsingPredicate:predicate]){// Enumerate each .png file in directory}
Recursively Enumerating Files In A Directory
If you want to go through each subdirectory at a particular location recursively,
you can do so by creating a File
object
with the enumerator(at
method:
letdirectoryURL:URLifletenumerator=FileManager.default.enumerator(atPath:directoryURL.path){forcaseletpathasStringinenumerator{// Skip entries with '_' prefix, for exampleifpath.hasPrefix("_"){enumerator.skipDescendants()}}}
NSFileManager*fileManager=[NSFileManagerdefaultManager];NSURL*bundleURL=[[NSBundlemainBundle]bundleURL];NSDirectoryEnumerator*enumerator=[fileManagerenumeratorAtURL:bundleURLincludingPropertiesForKeys:@[NSURLNameKey,NSURLIsDirectoryKey]options:NSDirectoryEnumerationSkipsHiddenFileserrorHandler:^BOOL(NSURL*url,NSError*error){if(error){NSLog(@"[Error] %@ (%@)",error,url);returnNO;}returnYES;}];NSMutableArray*mutableFileURLs=[NSMutableArrayarray];for(NSURL*fileURLinenumerator){NSString*filename;[fileURLgetResourceValue:&filenameforKey:NSURLNameKeyerror:nil];NSNumber*isDirectory;[fileURLgetResourceValue:&isDirectoryforKey:NSURLIsDirectoryKeyerror:nil];// Skip directories with '_' prefix, for exampleif([filenamehasPrefix:@"_"]&&[isDirectoryboolValue]){[enumeratorskipDescendants];continue;}if(![isDirectoryboolValue]){[mutableFileURLsaddObject:fileURL];}}
Creating a Directory
To create a directory,
call the method create
.
In Unix parlance, setting the with
parameter to true
is equivalent to passing the -p
option to mkdir
.
tryFileManager.default.createDirectory(at:directoryURL,withIntermediateDirectories:true,attributes:nil)
NSFileManager*fileManager=[NSFileManagerdefaultManager];NSString*documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];NSString*imagesPath=[documentsPathstringByAppendingPathComponent:@"images"];if(![fileManagerfileExistsAtPath:imagesPath]){NSError*error=nil;[fileManagercreateDirectoryAtPath:imagesPathwithIntermediateDirectories:NOattributes:nilerror:&error];}
Deleting a File or Directory
If you want to delete a file or directory,
call remove
:
letfileURL:URLtryFileManager.default.removeItem(at:fileURL)
NSFileManager*fileManager=[NSFileManagerdefaultManager];NSString*documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];NSString*filePath=[documentsPathstringByAppendingPathComponent:@"image.png"];NSError*error=nil;if(![fileManagerremoveItemAtPath:filePatherror:&error]){NSLog(@"[Error] %@ (%@)",error,filePath);}
FileManagerDelegate
File
may optionally set a delegate
to verify that it should perform a particular file operation.
This is a convenient way to audit all file operations in your app,
and a good place to factor out and centralize business logic,
such as which files to protect from deletion.
There are four operations covered by the
FileManagerDelegate
protocol:
moving, copying, removing, and linking items —
each with variations for working with paths and URLs,
as well as how to proceed after an error occurs:
If you were wondering when you might create your own File
rather than using this shared instance,
this is it.
From the documentation:
You should associate your delegate with a unique instance of the
File
class, as opposed to the shared instance.Manager
classCustomFileManagerDelegate:NSObject,FileManagerDelegate{funcfileManager(_fileManager:FileManager,shouldRemoveItemAtURL:URL)->Bool{// Don't delete PDF filesreturnURL.pathExtension!="pdf"}}// Maintain strong references to fileManager and delegateletfileManager=FileManager()letdelegate=CustomFileManagerDelegate()fileManager.delegate=delegate
NSFileManager*fileManager=[[NSFileManageralloc]init];fileManager.delegate=delegate;NSURL*bundleURL=[[NSBundlemainBundle]bundleURL];NSArray*contents=[fileManagercontentsOfDirectoryAtURL:bundleURLincludingPropertiesForKeys:@[]options:NSDirectoryEnumerationSkipsHiddenFileserror:nil];for(NSString*filePathincontents){[fileManagerremoveItemAtPath:filePatherror:nil];}// CustomFileManagerDelegate.m#pragma mark - NSFileManagerDelegate
-(BOOL)fileManager:(NSFileManager*)fileManagershouldRemoveItemAtURL:(NSURL*)URL{return![[[URLlastPathComponent]pathExtension]isEqualToString:@"pdf"];}
When you write an app that interacts with a file system, you don’t know if it’s an HDD or SSD or if it’s formatted with APFS or HFS+ or something else entirely. You don’t even know where the disk is: it could be internal or in a mounted peripheral, it could be network-attached, or maybe floating around somewhere in the cloud.
The best strategy for ensuring that things work
across each of the various permutations
is to work through File
and its related Foundation APIs.