URL in Swift

When the url contains chinese encoding. You should convert it to UTF-8 encoding, otherwise it will be nil

1 let urlstring0 = "http://www.baidu.com/时间/地点/"
2 let urlstring1 = "http://www.baidu.com/abc/def"
3 
4 let url0 = NSURL(string: urlstring0)
5 let url1 = NSURL(string: urlstring0.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
6 
7 let url2 = NSURL(string: urlstring1)
8 let url3 = NSURL(string: urlstring1.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
9 let urlstring2 = String(url1!).stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

Result:

你可能感兴趣的:(URL in Swift)