【问题标题】:What native class exists for URL building in iOS Objective-C?iOS Objective-C 中的 URL 构建存在哪些本机类?
【发布时间】:2019-11-16 07:23:26
【问题描述】:

在许多框架(或许多现代编程语言的核心语言)中,有一些工具可以轻松构建 URL。例如,假设您有一个架构 (http)、一个域 (www.example.com)、一个 API 基本路径 (/api/v1/),现在您想为不同的 API 端点生成多个 URL:

  • http://www.example.com/api/v1/customers
  • http://www.example.com/api/v1/customers/1
  • http://www.example.com/api/v1/orders
  • http://www.example.com/api/v1/orders/1

在 Android 上,这可以使用 Uri.Builder 来完成:

public MyClass()
{
    this.builder = new Uri.Builder()
                       .scheme("http")
                       .authority("www.exmaple.com")
                       .path("/api/v1/");
}

public void customerList()
{
    request(this.builder.appendPath("customers").build().toString());
}

public void getCustomer(int id)
{
    request(this.builder.appendPath("customers/" + id).build().toString());
}

public void orderList()
{
    request(this.builder.appendPath("orders").build().toString());
}

public void getOrder(int id)
{
    request(this.builder.appendPath("orders/" + id).build().toString());
}

为此目的,iOS 中存在哪些推论类?一些快速的谷歌搜索无法找到答案,我在 StackOverflow 上找到的只是 [Building up a URL in Objective-C question),它只使用字符串格式。理想情况下,我宁愿避免使用字符串格式化解决方案,因为例如,如果有人为域输入 www.example.com/(带有额外的斜杠),它可能会中断,因为没有上下文,也没有验证输入的类。

我想NSURL 可能有一些我正在寻找的功能,但无法弄清楚如何正确使用它。 Apple's documentation on the class 提到 URL 拥有方案、用户、密码、主机、端口等(我希望从适当的 URL 构建器中获得的所有成员),但没有提到如何设置它们。

【问题讨论】:

    标签: ios objective-c cocoa-touch nsurl


    【解决方案1】:

    您正在寻找NSURLComponents,您可以从组成部分构造URL,并可以在NSURLNSURLComponents 对象之间进行转换。

    【讨论】:

    • 你能提供一个使用NSURLComponents的代码示例吗?我刚刚尝试了NSURLComponents *test = [[NSURLComponents alloc] init]; test.scheme = "https"; test.host = "www.example.com"; NSLog([[test URL] absoluteString]); 之类的东西,它记录了(null) - 所以我一定错过了一些东西。
    • @stevendesu - 您使用的是什么编译器,您的代码片段会产生一些应该报告的错误。你有 C 字符串而不是 Obj-C 字符串,NSLog 应该真的有一种格式(没有一个你的 URL 字符串被视为格式,如果它有任何% ......)。片段NSURLComponents *test = [[NSURLComponents alloc] init]; test.scheme = @"https"; test.host = @"www.example.com"; NSLog(@"%@", [[test URL] absoluteString]); 工作正常。
    • 很好地掌握了 C 字符串。我没有复制/粘贴代码,而是重新输入了代码,因为 SO cmets 没有良好的格式,而且我经常忘记我的 @ 符号(必须稍后在编译器抱怨时添加它们)。 NSLog 上缺少格式字符串可能是我的问题。我对 Objective-C 很陌生,所以我现在犯了很多新手错误。当我星期一回到办公室时,我会试一试。
    【解决方案2】:

    您是对的,您正在寻找NSURL class,它处理所有 Mac 平台的 URL。而且您使用它的方式或多或少与您在 Android 代码中的使用方式相同:

    NSURL * url = [NSURL URLWithString:@"http://www.example.com/api/v1"];
    NSURL * urlCust1 = [url URLByAppendingPathComponent:@"customers"];
    NSURL * urlCust2 = [url URLByAppendingPathComponent:[NSString stringWithFormat:@"customers/%@",Cid]];
    NSURL * urlOrder = [url URLByAppendingPathComponent:@"orders"];
    NSURL * urlorderFull = [urlOrder URLByAppendingPathComponent:Oid];
    NSURL * basePath = [[url URLByDeletingLastPathComponent] URLByDeletingLastPathComponent];
    

    NSURL 负责斜线分隔符,因此您不需要跟踪它们(如果您在字符串中忘记了它,NSURL 将添加它;如果您在接收器中有一个结束斜杠,并且在新组件,NSURL 将压缩其中一个)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2011-09-23
      • 1970-01-01
      相关资源
      最近更新 更多