Created
April 23, 2019 11:07
-
-
Save digoreis/575430a1c52cdbcba7fa7fc5534e0b02 to your computer and use it in GitHub Desktop.
Small abstraction for url scheme
This file contains hidden or 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
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