在 Objective-C 中获取 NSURL 的一部分

发布于 2024-09-18 09:00:13 字数 188 浏览 7 评论 0原文

我有一个 NSString,其值为

http://digg.com/news/business/24hr

How can I get everything before the 3rd level?

http://digg.com/news/

I have an NSString with the value of

http://digg.com/news/business/24hr

How can I get everything before the 3rd level?

http://digg.com/news/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带刺的爱情 2024-09-25 09:00:13

请注意,这不完全是第三层。 URL 的分割方式如下:

  • 协议或方案(此处为 http)、
  • :// 分隔符、
  • 用户名和密码(此处没有,但它可以是username:password@hostname
  • 主机名(这里是digg.com
  • 端口(在:80之后例如域名)
  • 路径(此处为 /news/business/24hr
  • 参数字符串(分号后面的任何内容)
  • 查询字符串(如果您有 GET 参数,例如 ?foo=bar&baz=frob)
  • 片段(如果链接中有锚点,例如 #foobar)。

一个“功能齐全”的 URL 看起来像这样:

http://foobar:[email protected]:8080/some/path/file.html;params-here?foo=bar#baz

NSURL 具有广泛的访问器。您可以在 NSURL 类,访问 URL 的各个部分部分。快速参考:

  • -[NSURL schema] = http
  • -[NSURL resourceSpecifier] =(从 // 到 URL 末尾的所有内容)
  • -[NSURL user ] = foobar
  • -[NSURL 密码] = nicate
  • -[NSURL 主机] = example.com
  • -[NSURL 端口] = 8080
  • 章file.html"] (注意首字母 / 是其中的一部分)
  • -[NSURL lastPathComponent] = file.html
  • -[NSURL pathExtension] = html
  • - [NSURL参数字符串] = params-here
  • -[NSURL查询] = foo=bar
  • -[NSURL片段] = baz

不过,你想要什么,是这样的:

NSURL* url = [NSURL URLWithString:@"http://digg.com/news/business/24hr"];
NSString* reducedUrl = [NSString stringWithFormat:
    @"%@://%@/%@",
    url.scheme,
    url.host,
    url.pathComponents[1]];

对于您的示例 URL,您似乎想要的是协议、主机和第一个路径组件。 (-[NSString pathComponents] 返回的数组中索引 0 处的元素只是“/”,因此您需要索引 1 处的元素。其他斜杠将被丢弃。)

This isn't exactly the third level, mind you. An URL is split like that way:

  • the protocol or scheme (here, http)
  • the :// delimiter
  • the username and the password (here there isn't any, but it could be username:password@hostname)
  • the host name (here, digg.com)
  • the port (that would be :80 after the domain name for instance)
  • the path (here, /news/business/24hr)
  • the parameter string (anything that follows a semicolon)
  • the query string (that would be if you had GET parameters like ?foo=bar&baz=frob)
  • the fragment (that would be if you had an anchor in the link, like #foobar).

A "fully-featured" URL would look like this:

http://foobar:[email protected]:8080/some/path/file.html;params-here?foo=bar#baz

NSURL has a wide range of accessors. You may check them in the documentation for the NSURL class, section Accessing the Parts of the URL. For quick reference:

  • -[NSURL scheme] = http
  • -[NSURL resourceSpecifier] = (everything from // to the end of the URL)
  • -[NSURL user] = foobar
  • -[NSURL password] = nicate
  • -[NSURL host] = example.com
  • -[NSURL port] = 8080
  • -[NSURL path] = /some/path/file.html
  • -[NSURL pathComponents] = @["/", "some", "path", "file.html"] (note that the initial / is part of it)
  • -[NSURL lastPathComponent] = file.html
  • -[NSURL pathExtension] = html
  • -[NSURL parameterString] = params-here
  • -[NSURL query] = foo=bar
  • -[NSURL fragment] = baz

What you'll want, though, is something like that:

NSURL* url = [NSURL URLWithString:@"http://digg.com/news/business/24hr"];
NSString* reducedUrl = [NSString stringWithFormat:
    @"%@://%@/%@",
    url.scheme,
    url.host,
    url.pathComponents[1]];

For your example URL, what you seem to want is the protocol, the host and the first path component. (The element at index 0 in the array returned by -[NSString pathComponents] is simply "/", so you'll want the element at index 1. The other slashes are discarded.)

踏雪无痕 2024-09-25 09:00:13

使用 Playground ,语言敏捷,我明白了!
Playground 提供了一种交互式方式来查看其实际情况。我希望你能喜欢做同样的事情,这是学习 NSURL(iOS 中的一个重要主题)的有趣方式。

Using Playground , language swift, I got this !
Playground provides an interactive way of seeing this in action. I hope you can enjoy doing the same, a fun way to learn NSURL an important topic in iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文