Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active January 6, 2018 23:34
Show Gist options
  • Save KentarouKanno/a842c4a87359815c28b3164a42b56ea1 to your computer and use it in GitHub Desktop.
Save KentarouKanno/a842c4a87359815c28b3164a42b56ea1 to your computer and use it in GitHub Desktop.
/// 引数からURLを作成して返却する
///
/// - Parameters:
///   - path: パス文字列
///   - args: パラメーター(Dictionary)
/// - Returns: URL
public func url(_ path: String, args: Dictionary<String, String>? =  nil) -> URL {
    func joinPath(_ k: String...) -> URL {
        return URL(string: ([baseUrl] + k).joined(separator: ""))!
    }
    
    if let args = args {
        return joinPath(path + "?" + args.map({
            "\($0.0.urlEscapedString)=\($0.1.urlEscapedString)"
        }).joined(separator: "&"))
    } else {
        return joinPath(path)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment