IOS 開(kāi)發(fā)之NSURL基本操作
成都創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)提供萬(wàn)年企業(yè)網(wǎng)站建設(shè),專(zhuān)注與網(wǎng)站設(shè)計(jì)、做網(wǎng)站、html5、小程序制作等業(yè)務(wù)。10年已為萬(wàn)年眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。
NSURL其實(shí)就是我們?cè)跒g覽器上看到的網(wǎng)站地址,這不就是一個(gè)字符串么,為什么還要在寫(xiě)一個(gè)NSURL呢,主要是因?yàn)榫W(wǎng)站地址的字符串都比較復(fù)雜,包括很多請(qǐng)求參數(shù),這樣在請(qǐng)求過(guò)程中需要解析出來(lái)每個(gè)部門(mén),所以封裝一個(gè)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]);
結(jié)果:
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)
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!