2016年7月10日 | Leave a comment 问题: 把网址作为http get 参数时,需要把网址进行 encodeURIComponent : 如: let url=”http://m.news.baidu.com/news?fr=mohome&ssid=0&from=1014702t&uid=&pu=sz42001″ let ret=encodeURIComponent(url) // ret: http%3A%2F%2Fm.news.baidu.com%2Fnews%3Ffr%3Dmohome%26ssid%3D0%26from%3D1014702t%26uid%3D%26pu%3Dsz42001 解决: extension String{ func encodeURIComponent()->String{ let set=NSMutableCharacterSet(); set.formUnionWithCharacterSet(NSMutableCharacterSet.alphanumericCharacterSet()) set.formUnionWithCharacterSet(NSMutableCharacterSet(charactersInString:"-_.!~*'()")) let ret = self.stringByAddingPercentEncodingWithAllowedCharacters(set)! return ret } } 123456789 extension String{ func encodeURIComponent()->String{ let set=NSMutableCharacterSet(); set.formUnionWithCharacterSet(NSMutableCharacterSet.alphanumericCharacterSet()) set.formUnionWithCharacterSet(NSMutableCharacterSet(charactersInString:"-_.!~*'()")) let ret = self.stringByAddingPercentEncodingWithAllowedCharacters(set)! return ret }} 参考: http://www.cnblogs.com/snandy/archive/2013/01/14/2859883.html