【问题标题】:NSString's stringByAppendingPathComponent: removes a '/' in http://NSString 的 stringByAppendingPathComponent:删除 http:// 中的 '/'
【发布时间】:2010-04-05 16:35:11
【问题描述】:

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

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

让这段代码在操作系统之间工作的解决方案是使用

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

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

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


编辑

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

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

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

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

【问题讨论】:

  • 这不适用于带有服务前缀的文件路径。例如,file:///Users/myuser/Documents 将像您的 Web URL 示例一样被破坏。很难找到这一点。

标签: iphone cocoa cocoa-touch macos nsstring


【解决方案1】:

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

在我看来,您只想弄乱路径而不是整个 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 方法的工作方式相同。无论如何,希望它可以帮助您朝着正确的方向前进。

【讨论】:

  • 我喜欢这个答案,但可能会建议通过将新方法设为 NSURL 类别来进行改进?
  • 小心。有了这个,我相信你会失去一个自定义端口。例如,example.com:3003 ==> example.com
【解决方案2】:

URLByAppendingPathComponent

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

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

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

URLWithString:relativeToURL:

另外,还有URLWithString:relativeToURL:,它不会逃逸。因此,如果 url 组件已经被转义,请使用:

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

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

【讨论】:

    【解决方案3】:

    NSString Reference 这么说 stringByAppendingPathComponent:

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

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

    改用-stringByAppendingString:

    【讨论】:

    • 是的,我在发布问题后注意到文档中的注释。请参阅我的编辑。我可以使用stringByAppendingString: 是的。唯一阻止我这样做的是,我不想编写代码来检查是否还应该在字符串之前附加 / ,这是 NSURL 的 URLByAppendingPathComponent: 为你做的。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:
      + (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。

      【讨论】:

        猜你喜欢
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多