Skip to content

Instantly share code, notes, and snippets.

@tobiassteenweg
Created July 25, 2015 11:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tobiassteenweg/02c58e8fe7d4c9cede3c to your computer and use it in GitHub Desktop.
Save tobiassteenweg/02c58e8fe7d4c9cede3c to your computer and use it in GitHub Desktop.
encodeURIComponent() in swift
extension String {
func encodeURIComponent() -> String? {
var characterSet = NSMutableCharacterSet.alphanumericCharacterSet()
characterSet.addCharactersInString("-_.!~*'()")
return self.stringByAddingPercentEncodingWithAllowedCharacters(characterSet)
}
}
@soeminnminn
Copy link

soeminnminn commented Apr 2, 2018

// Swift 4

extension String {
    func encodeURIComponent() -> String? {
        var characterSet = CharacterSet.alphanumerics
        characterSet.insert(charactersIn: "-_.!~*'()")
        return self.addingPercentEncoding(withAllowedCharacters: characterSet)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment