Подтвердить что ты не робот

Objective-C код для создания относительного пути, заданного файлом и каталогом

Учитывая путь к файлу и путь к каталогу как NSString s, имеет ли кто-нибудь код Objective-C для создания пути к файлу относительно каталога?

Например, с учетом каталога /tmp/foo и файла /tmp/bar/test.txt код должен создать ../bar/test.txt.

Я знаю, что Python, по крайней мере, имеет способ сделать это: os.path.relpath.

4b9b3361

Ответ 1

Вместо того, чтобы продолжать защищать, зачем мне это нужно, я решил просто написать его и поделиться. Я основывал это на реализации Python os.path.relpath на 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

Обратите внимание, что в некоторых случаях это неправильно обрабатывается. Это случается, чтобы обрабатывать все случаи, в которых я нуждаюсь. Вот скудный unit test я использовал для проверки правильности:

@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

Ответ 2

Есть ли какая-то причина, по которой вы не можете просто использовать полный путь? imageNamed: полностью поддерживает это. Корень - основной комплект вашего приложения.

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

Ответ 3

Вот быстрая версия кода Hilton

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)
    }
}