Skip to content

Instantly share code, notes, and snippets.

@LucasVidal
Created September 28, 2015 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucasVidal/5d1d75b461765cb97e06 to your computer and use it in GitHub Desktop.
Save LucasVidal/5d1d75b461765cb97e06 to your computer and use it in GitHub Desktop.
var tempText = ":a/b?c,d=e&f"
var replacements = [
(":","%3A"),
("/","%2F"),
("?","%3F"),
(",","%2C"),
("=","%3D"),
("&","%26")]
extension String {
func encode(replacements: [(String,String)]) -> String? {
if let urlEncoded = stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) {
return replacements.reduce(urlEncoded) {
$0.stringByReplacingOccurrencesOfString($1.0, withString: $1.1)
}
}
return .None
}
}
tempText.encode(replacements)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment