Skip to content

Instantly share code, notes, and snippets.

@anoop4real
Created May 19, 2017 18:19
Show Gist options
  • Save anoop4real/2e37ba6bb4868af60b876ceaaeaca9a0 to your computer and use it in GitHub Desktop.
Save anoop4real/2e37ba6bb4868af60b876ceaaeaca9a0 to your computer and use it in GitHub Desktop.
Swift 3 String to URLRequest extension
extension String{
func asURLRequest()->URLRequest?{
var urlRequest:URLRequest?
if self.isValidForUrl(){
if let url = URL(string: self){
urlRequest = URLRequest(url: url)
}
}else{
urlRequest = nil
}
return urlRequest
}
func isValidForUrl() -> Bool {
if(self.hasPrefix("http") || self.hasPrefix("https")) {
return true
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment