当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS 开发之NSURL基本操作

IOS 开发之NSURL基本操作

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios 开发之nsurl基本操作

nsurl其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个nsurl呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个nsurl,操作很方便:

nsurl *url = [nsurl urlwithstring:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709"]; 
   
  nslog(@"scheme: %@", [url scheme]); 
   
  nslog(@"host: %@", [url host]); 
   
  nslog(@"port: %@", [url port]); 
   
  nslog(@"path: %@", [url path]); 
   
  nslog(@"relative path: %@", [url relativepath]); 
   
  nslog(@"path components as array: %@", [url pathcomponents]); 
   
  nslog(@"parameter string: %@", [url parameterstring]); 
   
  nslog(@"query: %@", [url query]); 
   
  nslog(@"fragment: %@", [url fragment]); 
   
  nslog(@"user: %@", [url user]); 
   
  nslog(@"password: %@", [url password]); 

结果:

2012-08-29 15:52:23.781 nsurl[3560:f803] scheme: http 
2012-08-29 15:52:32.793 nsurl[3560:f803] host: www.baidu.com 
2012-08-29 15:52:39.102 nsurl[3560:f803] port: (null) 
2012-08-29 15:52:42.590 nsurl[3560:f803] path: /s 
2012-08-29 15:52:52.516 nsurl[3560:f803] relative path: /s 
2012-08-29 15:53:05.576 nsurl[3560:f803] path components as array: ( 
  "/", 
  s 
) 
2012-08-29 15:53:32.861 nsurl[3560:f803] parameter string: (null) 
2012-08-29 15:53:37.528 nsurl[3560:f803] query: tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709 
2012-08-29 15:53:52.942 nsurl[3560:f803] fragment: (null) 
2012-08-29 15:53:54.539 nsurl[3560:f803] user: (null) 
2012-08-29 15:53:57.808 nsurl[3560:f803] password: (null) 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网