Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created January 7, 2021 01:23
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 sturdysturge/210b60db2de393190a2d62275da010e8 to your computer and use it in GitHub Desktop.
Save sturdysturge/210b60db2de393190a2d62275da010e8 to your computer and use it in GitHub Desktop.
extension String {
/// Remove all characters not allowed in a URL from the string
/// - Precondition: String must not be empty and contain at least one character allowed in a URL
/// - Postcondition: A valid String for creating a URL
var urlString: String {
precondition(self.isEmpty == false, "String cannot be empty")
let returnValue = self.filter { $0.isAllowedInURL }
precondition(URL(string: returnValue) != nil, "urlString \(returnValue) could not be used to make a URL")
return returnValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment