Hilton Campbell
Hilton Campbell

Reputation: 6095

Objective-C code to generate a relative path given a file and a directory

Given a file path and a directory path as NSStrings, does anyone have Objective-C code to generate a path to the file, relative to the directory?

For example, given the directory /tmp/foo and the file /tmp/bar/test.txt, the code should produce ../bar/test.txt.

I know Python, at least, has a method to do this: os.path.relpath.

Upvotes: 13

Views: 5871

Answers (3)

Alejandro
Alejandro

Reputation: 3746

Here is a Swift version of Hilton's code

extension String {
    var pathComponents: [String] {
        return (self as NSString).pathComponents
    }

    static func pathWithComponents(components: [String]) -> String {
        return NSString.pathWithComponents(components)
    }

    func stringWithPathRelativeTo(anchorPath: String) -> String {
        let pathComponents = self.pathComponents
        let anchorComponents = anchorPath.pathComponents

        var componentsInCommon = 0
        for (c1, c2) in zip(pathComponents, anchorComponents) {
            if c1 != c2 {
                break
            }
            componentsInCommon += 1
        }

        let numberOfParentComponents = anchorComponents.count - componentsInCommon
        let numberOfPathComponents = pathComponents.count - componentsInCommon

        var relativeComponents = [String]()
        relativeComponents.reserveCapacity(numberOfParentComponents + numberOfPathComponents)
        for _ in 0..<numberOfParentComponents {
            relativeComponents.append("..")
        }
        relativeComponents.appendContentsOf(pathComponents[componentsInCommon..<pathComponents.count])

        return String.pathWithComponents(relativeComponents)
    }
}

Upvotes: 0

Hilton Campbell
Hilton Campbell

Reputation: 6095

Rather than continue to defend why I need this, I decided to just write it and share. I based this off of an implementation of Python's os.path.relpath at http://mail.python.org/pipermail/python-list/2009-August/1215220.html

@implementation NSString (Paths)

- (NSString*)stringWithPathRelativeTo:(NSString*)anchorPath {
    NSArray *pathComponents = [self pathComponents];
    NSArray *anchorComponents = [anchorPath pathComponents];

    NSInteger componentsInCommon = MIN([pathComponents count], [anchorComponents count]);
    for (NSInteger i = 0, n = componentsInCommon; i < n; i++) {
        if (![[pathComponents objectAtIndex:i] isEqualToString:[anchorComponents objectAtIndex:i]]) {
            componentsInCommon = i;
            break;
        }
    }

    NSUInteger numberOfParentComponents = [anchorComponents count] - componentsInCommon;
    NSUInteger numberOfPathComponents = [pathComponents count] - componentsInCommon;

    NSMutableArray *relativeComponents = [NSMutableArray arrayWithCapacity:
                                          numberOfParentComponents + numberOfPathComponents];
    for (NSInteger i = 0; i < numberOfParentComponents; i++) {
        [relativeComponents addObject:@".."];
    }
    [relativeComponents addObjectsFromArray:
     [pathComponents subarrayWithRange:NSMakeRange(componentsInCommon, numberOfPathComponents)]];
    return [NSString pathWithComponents:relativeComponents];
}

@end

Note that there are some cases this won't correctly handle. It happens to handle all the cases I need. Here is the skimpy unit test I used to verify correctness:

@implementation NSStringPathsTests

- (void)testRelativePaths {
    STAssertEqualObjects([@"/a" stringWithPathRelativeTo:@"/"], @"a", @"");
    STAssertEqualObjects([@"a/b" stringWithPathRelativeTo:@"a"], @"b", @"");
    STAssertEqualObjects([@"a/b/c" stringWithPathRelativeTo:@"a"], @"b/c", @"");
    STAssertEqualObjects([@"a/b/c" stringWithPathRelativeTo:@"a/b"], @"c", @"");
    STAssertEqualObjects([@"a/b/c" stringWithPathRelativeTo:@"a/d"], @"../b/c", @"");
    STAssertEqualObjects([@"a/b/c" stringWithPathRelativeTo:@"a/d/e"], @"../../b/c", @"");
    STAssertEqualObjects([@"/a/b/c" stringWithPathRelativeTo:@"/d/e/f"], @"../../../a/b/c", @"");
}

@end

Upvotes: 27

zpasternack
zpasternack

Reputation: 17898

Is there some reason you can't just use the full path? imageNamed: totally supports that. The root is your app's main bundle.

myImageView.image = [UIImage imageNamed:@"/Path/To/Some/File.png"];

Upvotes: 0

Related Questions