Skip to content

Instantly share code, notes, and snippets.

@digoreis
Created April 23, 2019 11:07
Show Gist options
  • Save digoreis/575430a1c52cdbcba7fa7fc5534e0b02 to your computer and use it in GitHub Desktop.
Save digoreis/575430a1c52cdbcba7fa7fc5534e0b02 to your computer and use it in GitHub Desktop.
Small abstraction for url scheme
import Foundation
enum LinkType: FormatterUrl {
case product(String)
case look(String)
func formatter(prefix: String) -> String {
switch self {
case .product(let id): return prefix.appending("://products/\(id)")
case .look(let id): return prefix.appending("://looks?order=1&collections=\(id)")
}
}
}
protocol FormatterUrl {
func formatter(prefix: String) -> String
}
extension URL {
init?(scheme: String, formatter: FormatterUrl ){
guard let url = URL(string: formatter.formatter(prefix: scheme)) else { return nil }
self = url
}
}
let product = URL(scheme: "app", formatter: LinkType.product("1234"))
let look = URL(scheme: "app", formatter: LinkType.look("1234"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment