Directories in Objective-C
Here’s the translation of the Go code to Objective-C, with explanations in Markdown format suitable for Hugo:
Our first program will demonstrate working with directories in Objective-C. Here’s the full source code:
#import <Foundation/Foundation.h>
@interface DirectoryManager : NSObject
+ (void)createEmptyFileAtPath:(NSString *)path;
+ (void)check:(NSError *)error;
+ (void)visitPath:(NSString *)path;
@end
@implementation DirectoryManager
+ (void)createEmptyFileAtPath:(NSString *)path {
NSData *emptyData = [NSData data];
NSError *error;
[emptyData writeToFile:path options:NSDataWritingAtomic error:&error];
[self check:error];
}
+ (void)check:(NSError *)error {
if (error) {
@throw [NSException exceptionWithName:NSGenericException reason:[error localizedDescription] userInfo:nil];
}
}
+ (void)visitPath:(NSString *)path {
BOOL isDirectory;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
NSLog(@" %@, isDirectory: %d", path, isDirectory);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
// Create a new sub-directory in the current working directory
[fileManager createDirectoryAtPath:@"subdir" withIntermediateDirectories:NO attributes:nil error:&error];
[DirectoryManager check:error];
// Create empty files
[DirectoryManager createEmptyFileAtPath:@"subdir/file1"];
// Create a hierarchy of directories
[fileManager createDirectoryAtPath:@"subdir/parent/child" withIntermediateDirectories:YES attributes:nil error:&error];
[DirectoryManager check:error];
[DirectoryManager createEmptyFileAtPath:@"subdir/parent/file2"];
[DirectoryManager createEmptyFileAtPath:@"subdir/parent/file3"];
[DirectoryManager createEmptyFileAtPath:@"subdir/parent/child/file4"];
// List directory contents
NSArray *contents = [fileManager contentsOfDirectoryAtPath:@"subdir/parent" error:&error];
[DirectoryManager check:error];
NSLog(@"Listing subdir/parent");
for (NSString *item in contents) {
BOOL isDirectory;
[fileManager fileExistsAtPath:[@"subdir/parent" stringByAppendingPathComponent:item] isDirectory:&isDirectory];
NSLog(@" %@, isDirectory: %d", item, isDirectory);
}
// Change directory
[fileManager changeCurrentDirectoryPath:@"subdir/parent/child"];
contents = [fileManager contentsOfDirectoryAtPath:@"." error:&error];
[DirectoryManager check:error];
NSLog(@"Listing subdir/parent/child");
for (NSString *item in contents) {
BOOL isDirectory;
[fileManager fileExistsAtPath:item isDirectory:&isDirectory];
NSLog(@" %@, isDirectory: %d", item, isDirectory);
}
// Change back to original directory
[fileManager changeCurrentDirectoryPath:@"../../.."];
// Walk directory recursively
NSLog(@"Visiting subdir");
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:@"subdir"];
NSString *path;
while ((path = [enumerator nextObject])) {
[DirectoryManager visitPath:[NSString stringWithFormat:@"subdir/%@", path]];
}
// Clean up
[fileManager removeItemAtPath:@"subdir" error:&error];
[DirectoryManager check:error];
}
return 0;
}
This program demonstrates various operations with directories in Objective-C:
We create a new sub-directory using
createDirectoryAtPath:withIntermediateDirectories:attributes:error:
.We use a helper method
createEmptyFileAtPath:
to create empty files.We create a hierarchy of directories using
createDirectoryAtPath:withIntermediateDirectories:attributes:error:
with thewithIntermediateDirectories
flag set toYES
.We list directory contents using
contentsOfDirectoryAtPath:error:
.We change the current working directory using
changeCurrentDirectoryPath:
.We walk a directory recursively using
NSDirectoryEnumerator
.Finally, we clean up by removing the created directory structure.
To run the program, compile it and then execute the resulting binary:
$ clang -framework Foundation directories.m -o directories
$ ./directories
Listing subdir/parent
child, isDirectory: 1
file2, isDirectory: 0
file3, isDirectory: 0
Listing subdir/parent/child
file4, isDirectory: 0
Visiting subdir
subdir, isDirectory: 1
subdir/file1, isDirectory: 0
subdir/parent, isDirectory: 1
subdir/parent/child, isDirectory: 1
subdir/parent/child/file4, isDirectory: 0
subdir/parent/file2, isDirectory: 0
subdir/parent/file3, isDirectory: 0
This example demonstrates how to work with directories in Objective-C, including creating, listing, and traversing directory structures.