Skip to content

Instantly share code, notes, and snippets.

@sneakyness
Created June 9, 2017 01: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 sneakyness/fc4bcabb4f2bbf9dec52c151f1c9fac8 to your computer and use it in GitHub Desktop.
Save sneakyness/fc4bcabb4f2bbf9dec52c151f1c9fac8 to your computer and use it in GitHub Desktop.
func queryItems(dict:[String:String]) -> [URLQueryItem] {
var items = [URLQueryItem]()
for (name, value) in dict {
items.append(URLQueryItem(name: name, value: value))
}
return items
}
@erica
Copy link

erica commented Jun 9, 2017

extension URLQueryItem: ExpressibleByDictionaryLiteral {
    public typealias Key = String
    public typealias Value = String
    public init(dictionaryLiteral elements: (String, String)...) {
        guard elements.count == 1
            else { fatalError("URLQueryItem requires single key-value pair") }
        self.init(name: elements.last!.0, value: elements.last!.1)
    }
}

let uq: URLQueryItem = ["key": "value"]

// and

let uqs: [URLQueryItem] = [["key1": "value1"], ["key2": "value2"], ["key3": "value3"]]

But not quite what you're looking for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment