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
NSFileManager
, especially on iOS. Apple is in a long-term process of closing off the file system to the User, and in the process formalizing how the programmer should/can access something. dot dot slashing your way around is a good way to run amok of their app - - - that said[NSString pathComponents]
will help you decompose them and start from thereimageNamed
). I realize I have the tools to craft this, but I expect someone has a well-debugged version in their toolkit which would be of general use to SO visitors. No sense reinventing the wheel.imageNamed:
- - - it will hold a copy of the image data until the app quits, use only for small, frequently and always used images - - - if that's not the case useimageWithContentsOfFile:
- - - i would never useimageNamed:
on something not in the app top level bundle or you end up with names with ..s in them …imageNamed:
. It now has excellent caching behavior, which is why I want to use it instead ofimageWithContentsOfFile:
.