-
-
Save sturdysturge/210b60db2de393190a2d62275da010e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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