在Swift中解码一个字符串

在Swift中解码一个字符串

问题描述:

如何在swift中解码字符串?该字符串现在为test%2Enl,但正确的格式为test.nl。

How can i decode a string in swift? The string is now test%2Enl, but the correct format is test.nl.

我现在有这个:

 let encodedData = encodedString.dataUsingEncoding(NSUTF16StringEncoding)!
 let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
 let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!

 return attributedString.string     


您可以使用 stringByReplacingPercentEscapesUsingEncoding

var properString = s.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

swift 3的更新:
使用 removePercentEncoding 而不是。