Decode a string in Swift

In Swift 3:

let encodedString = "test%2Enl"
let decodedString = encodedString.removingPercentEncoding!

Swift 3 removed a lot of "excess" wording in many of the Apple APIs.

In this case stringByRemovingPercentEncoding was simplified to removingPercentEncoding.


You can use stringByReplacingPercentEscapesUsingEncoding

var properString = s.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

Update for swift 3: Use removingPercentEncoding instead.


Or you can use stringByRemovingPercentEncoding?

From the docs:

Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.

let encodedString = "test%2Enl"
let decodedString = encodedString.stringByRemovingPercentEncoding!
println(decodedString)

This would print test.nl

Tags:

Ios

Swift