let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEnCoding(NSUTF8StringEnCoding)
这现在给了我一个警告,要求我使用
stringByAddingPercentEnCodingWithAllowedCharacters
我需要使用NSCharacterSet作为参数,但有这么多,我不能确定什么会给我与以前使用的方法相同的结果。
我想使用的示例网址将是这样
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字符集似乎包含设置修剪我的
网址。即,
The path component of a URL is the component immediately following the
host component (if present). It ends wherever the query or fragment
component begins. For example,in the URL
07000,the path component is
/index.PHP.
但我不想修剪它的任何方面。
当我使用我的String,例如myurlstring它会失败。
但是当使用下面的,那么没有问题。它用一些魔法编码字符串,我可以得到我的URL数据。
let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEnCoding(NSUTF8StringEnCoding)
就这样
Returns a representation of the String using a given enCoding to
determine the percent escapes necessary to convert the String into a
legal URL string
谢谢
对于给定的URL字符串等效于let urlwithPercentEscapes = myurlstring.stringByAddingPercentEscapesUsingEnCoding(NSUTF8StringEnCoding)
是字符集URLqueryAllowedCharacterSet
let urlwithPercentEscapes = myurlstring.stringByAddingPercentEnCodingWithAllowedCharacters( NSCharacterSet.URLqueryAllowedCharacterSet())
Swift 3:
let urlwithPercentEscapes = myurlstring.addingPercentEnCoding( withAllowedCharacters: .urlqueryAllowed)
它对URL字符串中问号后的所有内容进行编码。
由于方法stringByAddingPercentEnCodingWithAllowedCharacters可以返回nil,使用可选绑定如Leo Dabus的答案中建议的。
总结以上是内存溢出为你收集整理的如何在Swift 2.0中使用stringByAddingPercentEncodingWithAllowedCharacters()全部内容,希望文章能够帮你解决如何在Swift 2.0中使用stringByAddingPercentEncodingWithAllowedCharacters()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)