File Paths in Objective-C

Here’s the translation of the Go code to Objective-C, along with explanations in Markdown format suitable for Hugo:

In Objective-C, we don’t have a direct equivalent to Go’s filepath package. However, we can use NSString and NSFileManager to achieve similar functionality. Here’s how we can work with file paths in Objective-C:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // NSString provides methods for path manipulation
        NSString *p = [@"dir1" stringByAppendingPathComponent:@"dir2"];
        p = [p stringByAppendingPathComponent:@"filename"];
        NSLog(@"p: %@", p);

        // Join paths, removing redundant separators
        NSLog(@"%@", [@"dir1//" stringByAppendingPathComponent:@"filename"]);
        NSLog(@"%@", [[@"dir1" stringByAppendingPathComponent:@".."] stringByAppendingPathComponent:[@"dir1" stringByAppendingPathComponent:@"filename"]]);

        // Get directory and file components
        NSLog(@"Dir(p): %@", [p stringByDeletingLastPathComponent]);
        NSLog(@"Base(p): %@", [p lastPathComponent]);

        // Check if path is absolute
        NSLog(@"%@", [NSFileManager.defaultManager isAbsolutePath:@"dir/file"] ? @"YES" : @"NO");
        NSLog(@"%@", [NSFileManager.defaultManager isAbsolutePath:@"/dir/file"] ? @"YES" : @"NO");

        NSString *filename = @"config.json";

        // Get file extension
        NSString *ext = [filename pathExtension];
        NSLog(@"%@", ext);

        // Remove file extension
        NSLog(@"%@", [filename stringByDeletingPathExtension]);

        // Find relative path
        NSString *base = @"a/b";
        NSString *target = @"a/b/t/file";
        NSString *rel = [target stringByReplacingOccurrencesOfString:[base stringByAppendingString:@"/"] withString:@""];
        NSLog(@"%@", rel);

        target = @"a/c/t/file";
        rel = [target stringByReplacingOccurrencesOfString:[base stringByAppendingString:@"/"] withString:@""];
        NSLog(@"%@", rel);
    }
    return 0;
}

In this Objective-C code:

  • We use stringByAppendingPathComponent: to join path components, which is similar to filepath.Join in Go.
  • stringByDeletingLastPathComponent and lastPathComponent are used to get the directory and file name, respectively.
  • isAbsolutePath: checks if a path is absolute.
  • pathExtension gets the file extension.
  • stringByDeletingPathExtension removes the file extension.
  • To find a relative path, we use string manipulation since Objective-C doesn’t have a direct equivalent to Go’s filepath.Rel.

When you compile and run this program, you should see output similar to:

p: dir1/dir2/filename
dir1/filename
dir1/dir1/filename
Dir(p): dir1/dir2
Base(p): filename
NO
YES
json
config
t/file
c/t/file

Note that the exact output may vary depending on the operating system, as Objective-C uses the native path separator for the platform it’s running on.

In Objective-C, path manipulation is often done using NSString methods, which provide a high-level interface for working with file paths. For more complex file operations, you would typically use NSFileManager.