macos NSString 的 stringByAppendingPathComponent: 删除 http:// 中的“/”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2579544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:51:34  来源:igfitidea点击:

NSString's stringByAppendingPathComponent: removes a '/' in http://

iphonecocoacocoa-touchmacosnsstring

提问by Jasarien

I've been modifying some code to work between Mac OS X and iPhone OS.

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

I came across some code that was using NSURL's URLByAppendingPathComponent:(added in 10.6), which as some may know, isn't available in the iPhone SDK.

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

My solution to make this code work between OS's is to use

我使此代码在操作系统之间工作的解决方案是使用

NSString *urlString = [myURL absoluteString];
urlString = [urlString stringByAppendingPathComponent:@"helloworld"];
myURL = [NSURL urlWithString:urlString];

The problem with this is that NSString's stringByAppendingPathComponent:seems to remove one of the /'s from the http:// part of the URL.

问题在于NSString'sstringByAppendingPathComponent:似乎从 URL 的 http:// 部分删除了 / 之一。

Is this intended behaviour or a bug?

这是预期的行为还是错误?



Edit

编辑

Ok, So I was a bit too quick in asking the question above. I re-read the documentation and it doessay:

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

Note that this method only works with file paths (not, for example, string representations of URLs)

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

However, it doesn't give any pointers in the right direction for what to do if you need to append a path component to a URL on the iPhone...

但是,如果您需要将路径组件附加到 iPhone 上的 URL,它并没有给出正确方向的任何指示...

I could always just do it manually, adding a /if necessary and the extra string, but I was looking to keep it as close to the original Mac OS X code as possible...

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

采纳答案by Chuck

I would implement a myURLByAppendingPathComponent:method on NSURL that does the same thing. The reason to give it a different name is so it doesn't override the Apple-provided method when Apple gets around to porting the 10.6 API to the iPhone (so the "my" is just an example — the point is that it's unlikely somebody else would write a method with that name).

我会myURLByAppendingPathComponent:在 NSURL 上实现一个做同样事情的方法。给它一个不同的名字的原因是,当 Apple 开始将 10.6 API 移植到 iPhone 时,它​​不会覆盖 Apple 提供的方法(所以“我的”只是一个例子——重点是它不太可能有人否则将编写一个具有该名称的方法)。

It seems to me you just want to mess with the path rather than the whole URL. Here's an untested example:

在我看来,您只想弄乱路径而不是整个 URL。这是一个未经测试的示例:

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

It would only work correctly with URLs that have file-like paths, but I'm pretty sure the Apple method works the same way. At any rate, hopefully it helps you in the right direction.

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

回答by nschum

URLByAppendingPathComponent

URLByAppendingPathComponent

Since iOS 4, URLByAppendingPathComponentis available on iOS and handles the two slashes correctly. (OS X had it since 10.6., as Chuck points out)

自 iOS 4 起,URLByAppendingPathComponent可在 iOS 上使用并正确处理两个斜杠。(正如 Chuck 指出的那样,OS X 从 10.6 开始就有了)

myURL = [myURL URLByAppendingPathComponent:@"hello world"]
// http://foo/bar/hello%20world

Note that unlike stringByAppendingPathComponent, this method escapes the argument.

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

URLWithString:relativeToURL:

URLWithString:relativeToURL:

Alternatively, there is URLWithString:relativeToURL:, which does not escape. So if the url component is already escaped, use:

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

myURL = [NSURL URLWithString:@"hello%20world" relativeToURL:myURL]
// http://foo/bar/hello%20world

Note that myURL needs to end with a slash here and the added segment must not have a leading slash.

请注意,此处 myURL 需要以斜线结尾,并且添加的段不能有前导斜线。

回答by David Gelhar

The NSString Referencesays this about stringByAppendingPathComponent:

NSString的参考说这约stringByAppendingPathComponent:

Note that this method only works with file paths (not, for example, string representations of URLs).

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

So, I think it's a case of "Don't Do That".

所以,我认为这是“不要那样做”的情况。

Use -stringByAppendingString:instead?

使用-stringByAppendingString:呢?

回答by Murvin

There is a simple work around. Using [NSString stringWithFormat:@"%@/%@", server, file] works fine.

有一个简单的解决方法。使用 [NSString stringWithFormat:@"%@/%@", server, file] 工作正常。

E.g. server : ftp://www.server.comand file : file.txt

例如服务器:ftp: //www.server.com和文件:file.txt

回答by Marlon Ruiz

+ (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;
}

As you see the code above is a class method and this permit use in anywhere without a instance of an object.

如您所见,上面的代码是一个类方法,这允许在没有对象实例的任何地方使用。

Note: Always the first object of the array must be a base url.

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