Created
May 19, 2017 18:19
-
-
Save anoop4real/2e37ba6bb4868af60b876ceaaeaca9a0 to your computer and use it in GitHub Desktop.
Swift 3 String to URLRequest extension
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{ | |
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