Skip to content

Instantly share code, notes, and snippets.

Created Jan 7, 2021
What would you like to do?
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