Iphone NSString';s stringByAppendingPathComponent:删除一个'/';在http中://

Iphone NSString';s stringByAppendingPathComponent:删除一个'/';在http中://,iphone,cocoa,cocoa-touch,macos,nsstring,Iphone,Cocoa,Cocoa Touch,Macos,Nsstring,我一直在修改一些代码,以便在Mac OS X和iPhone OS之间工作 我遇到了一些使用NSURL的urlbayAppendingPathComponent:(在10.6中添加)的代码,有些人可能知道,这些代码在iPhone SDK中不可用 我的解决方案是使用 NSString *urlString = [myURL absoluteString]; urlString = [urlString stringByAppendingPathComponent:@"helloworld"]; my

我一直在修改一些代码,以便在Mac OS X和iPhone OS之间工作

我遇到了一些使用
NSURL
urlbayAppendingPathComponent:
(在10.6中添加)的代码,有些人可能知道,这些代码在iPhone SDK中不可用

我的解决方案是使用

NSString *urlString = [myURL absoluteString];
urlString = [urlString stringByAppendingPathComponent:@"helloworld"];
myURL = [NSURL urlWithString:urlString];
问题是
NSString
stringByAppendingPathComponent:
似乎从URL的http://部分删除了一个/

这是预期行为还是错误


编辑 好的,所以我问上面的问题有点太快了。我重新阅读了文档,它确实说:

请注意,此方法仅适用于文件路径(例如,不适用于URL的字符串表示)

然而,如果你需要在iPhone上的URL中附加一个路径组件,它并没有给出任何正确方向的指针

我总是可以手动操作,在必要时添加一个/和额外的字符串,但我希望它尽可能接近原始的Mac OS X代码…

通过添加路径组件来说明这一点:stringByAppendingPathComponent:

请注意,此方法仅适用于 文件路径(例如,不是字符串 URL的表示形式)

所以,我认为这是一个“不要那样做”的例子


使用
-stringByAppendingString:
替代?

我将在NSURL上实现一个
myURLByAppendingPathComponent:
方法,该方法执行相同的操作。之所以给它起一个不同的名字,是因为当苹果开始将10.6API移植到iPhone上时,它不会覆盖苹果提供的方法(所以“my”只是一个例子——重点是其他人不太可能用这个名字编写方法)

在我看来,你只是想搞乱路径,而不是整个URL。以下是一个未经测试的示例:

- (NSURL *)myURLByAppendingPathComponent:(NSString *)component {
    NSString *newPath = [[self path] stringByAppendingPathComponent:component];
    return [[[NSURL alloc] initWithScheme: [self scheme] 
                                     host: [self host] 
                                     path: newPath]
                                     autorelease];
}

它只适用于具有类似文件路径的URL,但我非常确定Apple方法的工作方式是相同的。无论如何,希望它能帮助你朝着正确的方向前进。

有一个简单的解决方法。 使用[NSString stringWithFormat:@“%@/%@”,服务器,文件]可以正常工作

例如,服务器:和文件:file.txt

URLByAppendingPathComponent 从iOS 4开始,
URLByAppendingPathComponent
在iOS上可用,并正确处理两个斜杠。(正如查克指出的那样,OSX从10.6开始就有了它)

请注意,与
stringByAppendingPathComponent
不同,此方法将转义参数

URLWithString:relativeToURL: 或者,还有
URLWithString:relativeToURL:
,它不会转义。因此,如果url组件已转义,请使用:

myURL = [NSURL URLWithString:@"hello%20world" relativeToURL:myURL]
// http://foo/bar/hello%20world
请注意,myURL需要在此处以斜杠结尾,并且添加的段不能有前导斜杠

+ (NSString *)urlStringPathWithComponents:(NSArray *)paths
{
    NSString *url = @"";
    for( NSString *item in paths)
    {
        if([item isEqualToString:paths.firstObject])
        {
            url = [url stringByAppendingString:[NSString stringWithFormat:@"%@", item]];
        }else{
            url = [url stringByAppendingString:[NSString stringWithFormat:@"/%@", item]];
        }

    }
    return url;
}
正如您所看到的,上面的代码是一个类方法,它允许在没有对象实例的任何地方使用


注意:数组的第一个对象必须始终是基本url。

是的,我在发布问题后注意到文档中的注意。请参阅我的编辑。我可以使用
stringByAppendingString:
yes。唯一阻止我这样做的是,我不想编写代码来检查是否应该在字符串之前附加a/或a/或a/或B,NSURL的
URLByAppendingPathComponent:
为您做了这一点。我喜欢这个答案,但可能会建议将新方法作为NSURL的一个类别来进行改进。小心。我相信,有了这个,您将失去一个自定义端口。e、 但是,这在带有服务前缀的文件路径上不起作用。例如file:///Users/myuser/Documents 将与您的web URL示例一样被损坏。我是通过艰苦的努力才发现这一点的。
+ (NSString *)urlStringPathWithComponents:(NSArray *)paths
{
    NSString *url = @"";
    for( NSString *item in paths)
    {
        if([item isEqualToString:paths.firstObject])
        {
            url = [url stringByAppendingString:[NSString stringWithFormat:@"%@", item]];
        }else{
            url = [url stringByAppendingString:[NSString stringWithFormat:@"/%@", item]];
        }

    }
    return url;
}