Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone Objective-C代码生成给定文件和目录的相对路径_Iphone_Objective C_Ios_File - Fatal编程技术网

Iphone Objective-C代码生成给定文件和目录的相对路径

Iphone Objective-C代码生成给定文件和目录的相对路径,iphone,objective-c,ios,file,Iphone,Objective C,Ios,File,给定文件路径和目录路径为NSStrings,是否有人使用Objective-C代码生成相对于目录的文件路径 例如,给定目录/tmp/foo和文件/tmp/bar/test.txt,代码应该生成。/bar/test.txt 我知道Python至少有一种方法可以做到这一点:。有什么原因不能只使用完整路径吗imagename:完全支持这一点。根目录是应用程序的主捆绑包 myImageView.image = [UIImage imageNamed:@"/Path/To/Some/File.png"];

给定文件路径和目录路径为
NSString
s,是否有人使用Objective-C代码生成相对于目录的文件路径

例如,给定目录
/tmp/foo
和文件
/tmp/bar/test.txt
,代码应该生成
。/bar/test.txt


我知道Python至少有一种方法可以做到这一点:。

有什么原因不能只使用完整路径吗
imagename:
完全支持这一点。根目录是应用程序的主捆绑包

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

与其继续为我为什么需要这个辩护,我决定写下来和大家分享。我基于Python的
os.path.relpath
at的一个实现


这是希尔顿密码的快速版本

扩展字符串{
var路径组件:[字符串]{
返回(自身作为NSString)。路径组件
}
静态func路径WithComponents(components:[String])->String{
返回NSString.pathWithComponents(组件)
}
func stringWithPathRelativeTo(主播路径:String)->String{
设pathComponents=self.pathComponents
让anchorComponents=anchorPath.pathComponents
var componentsInCommon=0
用于拉链中的(c1、c2)(路径组件、锚点组件){
如果c1!=c2{
打破
}
组件sincommon+=1
}
让numberOfParentComponents=anchorComponents.count-Components通用
让numberOfPathComponents=pathComponents.count-componentsInCommon
var relativeComponents=[String]()
相对组件。保留容量(numberOfParentComponents+numberOfPathComponents)

对于0中的uu..当然我们都可以写一个(或挖一个),但你的要求会让人大吃一惊。你应该试着使用
NSFileManager
,尤其是在iOS上。苹果公司正在对用户关闭文件系统,并正在正式确定程序员应该/可以如何访问某个东西。点-点切割你的方式是一种让他们的应用程序疯狂运行的好方法----th在所说的
[NSString pathComponents]
将帮助您分解它们并从警告中指出的开始。对于我的特定用例,我只在iOS上的应用程序沙箱中操作。我有一个文件的完整路径,需要传递到一个API,该API假定一个相对于特定目录的路径,并且不接受完整路径()。我知道我有制作这个的工具,但我希望有人在他们的工具箱中有一个调试良好的版本,对这么多的访问者来说是通用的。重新发明轮子没有意义。你确定吗?我有一个很棒的六面轮子出售;-)---我确定你知道
图像命名:
--它将保存一份在应用程序退出之前,图像数据只用于小的、经常使用的图像---如果不是这样,请使用
imageWithContentsOfFile:
----我永远不会在应用程序顶级捆绑包之外的东西上使用
imageNamed:
,或者你最终得到的名称中有..s…这个轮子绝对值得重新发明,或者至少重新实现:)。我的应用程序至少需要iOS 4.2,因此我可以享受他们对
ImageName:
所做的所有错误修复。它现在具有出色的缓存性能,这就是为什么我想使用它而不是
imageWithContentsOfFile:
。如果有人一直使用这个版本,我会有点惊讶。我在Cocoa的典型用例中,这并不是一项常见的任务。通常,要么你从用户那里得到了一条路径(在这种情况下,你确切地知道你想要的路径),要么你从系统函数中得到了路径(在这种情况下,你确切地知道你想要的路径)或者您将路径硬编码到应用程序中。您需要从一个任意位置导出到另一个任意位置的相对路径,这是非常罕见的。
imagename:
只接受相对于应用程序主捆绑包根目录的路径。我想从应用程序的缓存目录加载图像,该目录位于应用程序包。我无法使用绝对路径(我已尝试)引用该文件,但我可以使用相对路径(我已尝试)引用它。不过,感谢您尝试帮助我找到解决方法。
@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
@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