Skip to content

Instantly share code, notes, and snippets.

@BjornRuud
Created December 7, 2017 14:24
Show Gist options
  • Save BjornRuud/0966bacaa5eca49ae6c45a3373a009dc to your computer and use it in GitHub Desktop.
Save BjornRuud/0966bacaa5eca49ae6c45a3373a009dc to your computer and use it in GitHub Desktop.
URL namespace to collect URLs in a convenient place.
extension URL {
enum MyURLNamespace {
// Static URL, always in memory
static let gitHub = URL(string: "https://github.com/BjornRuud")!
// Computed URL, created on demand
static var gitHubWithStaticParameter: URL {
let param = "SomeParameter"
return URL(string: "https://github.com/BjornRuud/\(param)")!
}
// Computed URL with parameters
static func gitHubHome(userID: String) -> URL {
return URL(string: "https://github.com/\(userID)")!
}
}
static let mynamespace = MyURLNamespace.self
}
// Usage:
let url = URL.mynamespace.gitHub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment