Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode a string in Swift

Tags:

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

I now have this:

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

 return attributedString.string     
like image 392
Kevinvs Avatar asked Feb 03 '15 23:02

Kevinvs


1 Answers

You can use stringByReplacingPercentEscapesUsingEncoding

var properString = s.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

Update for swift 3: Use removingPercentEncoding instead.

like image 188
Christian Avatar answered Sep 21 '22 05:09

Christian