關於NSURL的解析和編碼

2021-06-19 02:06:10 字數 853 閱讀 1623

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]);

將得到以下輸出:

沒什麼值得多說的~相當方便就能得到所要結果的方法~

另外,在由nsstring生成nsurl物件時,有可能會出現nsstring中包含百分各類括號,冒號等對於url來說d非法字元。如果直接進行轉換的話將得到nil。在對於複雜url進行轉換前,可以先實施對待轉換的nsstring傳送

stringbyaddingpercentescapesusingencoding

將其轉換為合法的url字串(其實目的就是保證非法字元用utf8編碼……)

比如這樣:

[crayon]

nsstring *fixedstr = [reqstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

[/crayon]

**:

關於 NSURL 的解析和編碼

nsurl url nsurl urlwithstring nslog scheme url scheme nslog host url host nslog port url port nslog path url path nslog relative path url relativepath...

關於gb2312編碼的xml解析

在ios專案中遇到這樣乙個問題,需要讀取遠端的xml檔案,進行解析,但是目標xml是gb2312編碼的,用system.text.encoding.getencoding gb2312 並不奏效。提示 encoding name gb2312 not supported 查了下才知道getencod...

關於字元的編碼和轉碼

在實際開發中,會經常遇到字元編碼的轉換,這裡介紹一種url和string相互轉換的方法 try catch unsupportedencodingexception e 列印結果 data1 e5 8c 97 e4 ba ac,data2 北京市 這樣就很清楚的知道了,urlencoder.enco...