Extensions that help reduce pain when building up Foundation.URL instances
This file contains 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
// | |
// url-extensions.swift | |
// | |
// Created by Guillaume Lessard | |
// Copyright (c) 2018 Guillaume Lessard. All rights reserved. | |
// | |
// https://gist.github.com/glessard/a2b2916aa74f082a58d50a674174f1a4 | |
// | |
import Foundation | |
extension URL | |
{ | |
func appendingPathComponents(_ strings: String...) -> URL | |
{ | |
var url = self | |
for string in strings | |
{ | |
url.appendPathComponent(string) | |
} | |
return url.absoluteURL | |
} | |
func appendingQueryItem(_ query: URLQueryItem) -> URL | |
{ | |
if var components = URLComponents(url: self, resolvingAgainstBaseURL: false) | |
{ | |
components.queryItems = [query] | |
if let url = components.url { return url.absoluteURL } | |
} | |
fatalError("couldn't handle url \(self) in \(#function)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment