在Objective-C中获取NSURL的一部分 [英] Get parts of a NSURL in objective-c

查看:125
本文介绍了在Objective-C中获取NSURL的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSString,值是

I have an NSString with the value of

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

如何获得3级之前的一切?

How can I get everything before the 3rd level?

http://digg.com/news/

推荐答案

请注意,这并不完全是第三级.像这样分割URL:

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

  • 协议或方案(此处为http)
  • ://分隔符
  • 用户名和密码(这里没有,但可以是username:password@hostname)
  • 主机名(此处为digg.com)
  • 端口(例如,域名后的:80)
  • 路径(此处为/news/business/24hr)
  • 参数字符串(分号后的任何内容)
  • 查询字符串(如果您具有诸如?foo=bar&baz=frob之类的GET参数,则为该字符串)
  • 片段(如果您在链接中有锚点,例如#foobar).
  • 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:nicate@example.com:8080/some/path/file.html;params-here?foo=bar#baz

NSURL具有广泛的访问器.您可以在

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] =(从//到URL末尾的所有内容)
  • -[NSURL user] = foobar
  • -[NSURL password] =尼古丁
  • -[NSURL host] = example.com
  • -[NSURL port] = 8080
  • -[NSURL path] =/some/path/file.html
  • -[NSURL pathComponents] = @ ["/","some","path","file.html"](请注意,开头的/是其中的一部分)
  • -[NSURL lastPathComponent] = file.html
  • -[NSURL pathExtension] = html
  • -[NSURL parameterString] = params-here
  • -[NSURL query] = foo = bar
  • -[NSURL fragment] = baz
  • -[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]];

对于您的示例URL,您似乎想要的是协议,主机和第一个路径组件. (-[NSString pathComponents]返回的数组中索引0处的元素只是"/",因此您需要索引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.)

这篇关于在Objective-C中获取NSURL的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆