Skip to content

Instantly share code, notes, and snippets.

@glessard
Created March 8, 2018 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glessard/a2b2916aa74f082a58d50a674174f1a4 to your computer and use it in GitHub Desktop.
Save glessard/a2b2916aa74f082a58d50a674174f1a4 to your computer and use it in GitHub Desktop.
Extensions that help reduce pain when building up Foundation.URL instances
//
// 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