URI转码

stringByAddingPercentEncodingWithAllowedCharacters方法总是搞不清楚后面的参数set是个啥。
说白了,就是一个字符集合+默认的所有中文,集合中出现的字符都会被URI编码。

以下两种表述是一样的

//invertedSet 功能大概就是将 列出的字符和所有中文组成新集合 再取其反集合。
//白话解释: 列举出的字符和所有中文都会被排除
NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:@"\"#%<>[\\]^`{|}"] invertedSet];
NSCharacterSet *set2 = [NSCharacterSet URLQueryAllowedCharacterSet];

stringByAddingPercentEncodingWithAllowedCharacters这个方法就是将允许的字符保留,其他的都转成%形式的URL编码,而上面的到的set是“列举出的字符和所有中文都会被排除”,所以这些字符会被转码,而未被列出来的则保留原状。

拓展常用的NSCharacterSet

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`

你可能感兴趣的:(URI转码)