IOS 开发之NSURL基本操作
IOS开发之NSURL基本操作
NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个NSURL,操作很方便:
NSURL*url=[NSURLURLWithString:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709"]; NSLog(@"Scheme:%@",[urlscheme]); NSLog(@"Host:%@",[urlhost]); NSLog(@"Port:%@",[urlport]); NSLog(@"Path:%@",[urlpath]); NSLog(@"Relativepath:%@",[urlrelativePath]); NSLog(@"Pathcomponentsasarray:%@",[urlpathComponents]); NSLog(@"Parameterstring:%@",[urlparameterString]); NSLog(@"Query:%@",[urlquery]); NSLog(@"Fragment:%@",[urlfragment]); NSLog(@"User:%@",[urluser]); NSLog(@"Password:%@",[urlpassword]);
结果:
2012-08-2915:52:23.781NSurl[3560:f803]Scheme:http 2012-08-2915:52:32.793NSurl[3560:f803]Host:www.baidu.com 2012-08-2915:52:39.102NSurl[3560:f803]Port:(null) 2012-08-2915:52:42.590NSurl[3560:f803]Path:/s 2012-08-2915:52:52.516NSurl[3560:f803]Relativepath:/s 2012-08-2915:53:05.576NSurl[3560:f803]Pathcomponentsasarray:( "/", s ) 2012-08-2915:53:32.861NSurl[3560:f803]Parameterstring:(null) 2012-08-2915:53:37.528NSurl[3560:f803]Query:tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709 2012-08-2915:53:52.942NSurl[3560:f803]Fragment:(null) 2012-08-2915:53:54.539NSurl[3560:f803]User:(null) 2012-08-2915:53:57.808NSurl[3560:f803]Password:(null)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!