我在 Swift 1.2 中使用了这个。
let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
现在这给出了一个警告,要求我使用
stringByAddingPercentEncodingWithAllowedCharacters
我需要将一个NSCharacterSet用作参数,但是有这么多选择,我无法确定哪个会给我与之前使用的方法相同的结果。
我想要使用的示例URL将像这样
http://www.mapquestapi.com/geocoding/v1/batch?key=YOUR_KEY_HERE&callback=renderBatch&location=Pottsville,PA&location=Red Lion&location=19036&location=1090 N Charlotte St, Lancaster, PA
URL 字符集用于编码,似乎包含可以修剪我的URL的集合。例如,在链接 http://www.example.com/index.php?key1=value1 中,路径组件为 /index.php。但是我不想修剪它的任何方面。当我使用字符串myurlstring
时,会失败。但是使用以下字符串就没有问题了,它会用一些魔法进行编码,然后我就可以获得我的URL数据。let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
它使用给定的编码返回字符串的表示形式,以确定百分号转义所需的内容,以将字符串转换为合法的URL字符串。谢谢。