IOS NSURL基本操作

來源:互聯網
上載者:User

標籤:

NSURL其實就是我們在瀏覽器上看到的網站地址,這不就是一個字串麼,為什麼還要在寫一個NSURL呢,主要是因為網站地址的字串都比較複雜,包括很多請求參數,這樣在請求過程中需要解析出來每個部門,所以封裝一個NSURL,操作很方便:

 

 

[cpp] view plaincopy 
  1. 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"];  
  2.       
  3.     NSLog(@"Scheme: %@", [url scheme]);  
  4.       
  5.     NSLog(@"Host: %@", [url host]);  
  6.       
  7.     NSLog(@"Port: %@", [url port]);  
  8.       
  9.     NSLog(@"Path: %@", [url path]);  
  10.       
  11.     NSLog(@"Relative path: %@", [url relativePath]);  
  12.       
  13.     NSLog(@"Path components as array: %@", [url pathComponents]);  
  14.       
  15.     NSLog(@"Parameter string: %@", [url parameterString]);  
  16.       
  17.     NSLog(@"Query: %@", [url query]);  
  18.       
  19.     NSLog(@"Fragment: %@", [url fragment]);  
  20.       
  21.     NSLog(@"User: %@", [url user]);  
  22.       
  23.     NSLog(@"Password: %@", [url password]);  

 

 

結果:

 

[cpp] view plaincopy 
    1. 2012-08-29 15:52:23.781 NSurl[3560:f803] Scheme: http  
    2. 2012-08-29 15:52:32.793 NSurl[3560:f803] Host: www.baidu.com  
    3. 2012-08-29 15:52:39.102 NSurl[3560:f803] Port: (null)  
    4. 2012-08-29 15:52:42.590 NSurl[3560:f803] Path: /s  
    5. 2012-08-29 15:52:52.516 NSurl[3560:f803] Relative path: /s  
    6. 2012-08-29 15:53:05.576 NSurl[3560:f803] Path components as array: (  
    7.     "/",  
    8.     s  
    9. )  
    10. 2012-08-29 15:53:32.861 NSurl[3560:f803] Parameter string: (null)  
    11. 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  
    12. 2012-08-29 15:53:52.942 NSurl[3560:f803] Fragment: (null)  
    13. 2012-08-29 15:53:54.539 NSurl[3560:f803] User: (null)  
    14. 2012-08-29 15:53:57.808 NSurl[3560:f803] Password: (null)  

IOS NSURL基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.