#25: Convert URL to String in Swift

A URL already knows how to represent itself as text. In most cases you want absoluteString:

let url = URL(string: "https://www.uraimo.com/swiftbites/")!
let text = url.absoluteString
// "https://www.uraimo.com/swiftbites/"

If you only need a specific component, use the dedicated properties instead:

url.host // "www.uraimo.com"
url.path // "/swiftbites/"

Prefer extracting components over manual string slicing whenever you need to inspect or transform URLs.


I'm also on Twitter and GitHub.

Subscribe via RSS.