Skip to content

Instantly share code, notes, and snippets.

@ha1f
Last active October 12, 2020 05:06
Show Gist options
  • Save ha1f/2f5873372153f9cda67fa344a2718a50 to your computer and use it in GitHub Desktop.
Save ha1f/2f5873372153f9cda67fa344a2718a50 to your computer and use it in GitHub Desktop.
/// JavaのStringBuilder的なインターフェースを想定
final class URLBuilder {
private var components: URLComponents
init?(string: String) {
guard let components = URLComponents(string: string) else {
return nil
}
self.components = components
}
@discardableResult
func append(_ queryItem: URLQueryItem) -> URLBuilder {
var newItems = components.queryItems ?? []
newItems.append(queryItem)
components.queryItems = newItems
return self
}
func build() -> URL? {
components.url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment